Changing renderPlot colors from gpkg file

Hi all,

New RStudio user here. I built a series of density maps in QGIS and imported them into RStudio as geopackage files. I can plot the datasets correctly using

 output$dataset <- renderPlot({ 
    plot(datasetInput()['Grand.Total'], main= "Cases by Volume")
  })

(note: the datasets are reactive) but have been having a hard time trying to change the colors. If I use col= topo.colors () or something similar, the colors do not mirror my density data and I lose my scale:

  output$dataset <- renderPlot({ 
    plot(datasetInput()['Grand.Total'], col= topo.colors (n= 5), main= "Cases by Volume")

If anyone has any suggestions on how to fix this, please let me know!
Many thanks!!

Hi @afwhite! Welcome!

The best way to get help with your app is to create a small, self-contained reproducible example that others can run in order to understand your problem and work on solutions.

That can be hard with complicated data inputs, so before you even go that far, it might be worth first testing if this is even a Shiny-related problem. Are you able to plot these data the way you expect outside of your app, in a regular R script?

Hello,

Thank you for the response! I haven't had an issue plotting the data itself. RStudio will read the geopackage files using filename <- st_read ("filename.gpkg") and I can plot them using the renderPlot function without a problem. I would just like to be able to edit the color set in order to make the differences in scale more distinguishable. Here's my app code below. Hopefully that's a little more clear- please let me know if you need more detail. Thank you again!

World <-st_read("Tot_World_Cases.gpkg")
US <-st_read("Tot_US_Cases.gpkg")


worlddata <- read.csv("worlddata.csv")
str(worlddata) 
mat1 <- as.matrix(worlddata)
str(mat1)

usdata <-read.csv("usdata.csv")
str(usdata)
mat2 <- as.matrix(usdata)
str(mat2)                  

ui <- fluidPage(
  
  titlePanel(tags$b("Call Data 2014-2018")),
 tags$br(),
       mainPanel(
                  tabsetPanel(type = "tabs",
                        tabPanel ("Maps",
                                  plotOutput("dataset", width = "700px", height = "600px"),
                                  tags$br(),
                        sidebarPanel(
                                  radioButtons('dataset','Choose a dataset:',choices = c ("US", "World")), helpText("Scale indicates call volume; white areas indicate no data"))),
                        
                        tabPanel("US Data", 
                                 selectizeInput('stateID', tags$h4 (tags$b('Please select a state:')), choices = c("Choose" = "", levels(usdata$State.Name))), 
                                 tableOutput("table1")),
                        
                        tabPanel ("World Data",
                                  selectizeInput('countryID', tags$h4 (tags$b('Please select a country:')), choices =  c("Choose" = "", levels (worlddata$Country.Name))),
                                  tableOutput("table2")),
                        
                        
                         tabPanel("Category Descriptions",
                                 tags$div (class = "header", checked =NA,
                                           tags$h3 (tags$b ("Category Descriptions:")), 
                                           tags$br(),
                                           tags$h4("Category Heading", style="color:blue"),
                                           tags$ul(
                                             list(
                                               tags$li ("Category Description")))
                                          
                       )))))
          


server <- function(input, output, session) {
 
   datasetInput <- reactive ({
    switch(input$dataset,
           "US"= US,
           "World"= World)
  })
   
   tab1 <- reactive ({
     usdata %>% 
       filter(State.Name == input$stateID)
   }) 
   
   tab2 <- reactive ({
     worlddata %>% 
       filter(Country.Name == input$countryID)
   }) 

  output$dataset <- renderPlot({ 
    plot(datasetInput()['Grand.Total'], main= "Cases by Volume")

  })

  output$table1 <- renderTable ({t(tab1() [1:1,3:10])}, bordered = TRUE, spacing = "s", width = "auto", rownames = TRUE, colnames = FALSE, na= "N/A")
  
  output$table2 <- renderTable ({ t(tab2 () [1:1,3:10])}, bordered = TRUE, spacing = "s", width = "auto", rownames = TRUE, colnames = FALSE, na= "N/A")

  }
shinyApp(ui = ui, server = server)

Here's what the plot output looks like:

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.