r - Fix range across choropleths in Shiny when using choroplethr package -


i have simple shiny app, code @ bottom of question.

the app allows @ years 2000, , 2001. in both cases, california darkest state, since has highest values (500 , 1000, respectively).

my issue set scale colors fixed across both years. notice california has dark blue first year (corresponding value of 1000).

enter image description here

notice california has exact same dark blue second year (corresponding value of 500).

enter image description here

when looking @ choropleths stand, easy miss fact value dropped in half across years (and occurs in different ways other states, of course). way fix range across plots. how can achieve this?

df <- structure(list(region = c("alabama", "alabama", "alaska", "alaska",                                  "arizona", "arizona", "arkansas", "arkansas", "california", "california",                                  "colorado", "colorado", "connecticut", "connecticut", "delaware",                                  "delaware", "district of columbia", "district of columbia", "florida",                                  "florida", "georgia", "georgia", "hawaii", "hawaii", "idaho",                                  "idaho", "illinois", "illinois", "indiana", "indiana", "iowa",                                  "iowa", "kansas", "kansas", "kentucky", "kentucky", "louisiana",                                  "louisiana", "maine", "maine", "maryland", "maryland", "massachusetts",                                  "massachusetts", "michigan", "michigan", "minnesota", "minnesota",                                  "mississippi", "mississippi", "missouri", "missouri", "montana",                                  "montana", "nebraska", "nebraska", "nevada", "nevada", "new hampshire",                                  "new hampshire", "new jersey", "new jersey", "new mexico", "new mexico",                                  "new york", "new york", "north carolina", "north carolina", "north dakota",                                  "north dakota", "ohio", "ohio", "oklahoma", "oklahoma", "oregon",                                  "oregon", "pennsylvania", "pennsylvania", "rhode island", "rhode island",                                  "south carolina", "south carolina", "south dakota", "south dakota",                                  "tennessee", "tennessee", "texas", "texas", "utah", "utah", "vermont",                                  "vermont", "virginia", "virginia", "washington", "washington",                                  "west virginia", "west virginia", "wisconsin", "wisconsin", "wyoming",                                  "wyoming"), date = c("2000", "2001", "2000", "2001", "2000",                                                       "2001", "2000", "2001", "2000", "2001", "2000", "2001", "2000",                                                       "2001", "2000", "2001", "2000", "2001", "2000", "2001", "2000",                                                       "2001", "2000", "2001", "2000", "2001", "2000", "2001", "2000",                                                       "2001", "2000", "2001", "2000", "2001", "2000", "2001", "2000",                                                       "2001", "2000", "2001", "2000", "2001", "2000", "2001", "2000",                                                       "2001", "2000", "2001", "2000", "2001", "2000", "2001", "2000",                                                       "2001", "2000", "2001", "2000", "2001", "2000", "2001", "2000",                                                       "2001", "2000", "2001", "2000", "2001", "2000", "2001", "2000",                                                       "2001", "2000", "2001", "2000", "2001", "2000", "2001", "2000",                                                       "2001", "2000", "2001", "2000", "2001", "2000", "2001", "2000",                                                       "2001", "2000", "2001", "2000", "2001", "2000", "2001", "2000",                                                       "2001", "2000", "2001", "2000", "2001", "2000", "2001", "2000",                                                       "2001"), value = c(19, 11, 83, 80, 87, 79, 87, 45, 1000, 500,                                                                          89, 163, 41, 101, 53, 3, 39, 55, 500, 347, 71, 89, 37, 43, 23,                                                                          41, 243, 175, 271, 215, 75, 3, 22, 33, 11, 15, 5, 55, 18, 60,                                                                          17, 79, 59, 61, 193, 165, 11, 65, 237, 299, 373, 233, 17, 7,                                                                          69, 21, 433, 81, 79, 63, 127, 95, 5, 111, 341, 373, 53, 53,                                                                          35, 63, 157, 81, 75, 35, 57, 23, 445, 511, 17, 15, 21, 79, 118,                                                                          88, 153, 167, 68, 471, 1, 83, 18, 8, 55, 21, 95, 35, 33, 47,                                                                          13, 23, 7, 17)), .names = c("region", "date", "value"), row.names = c(na,                                                                                                                                                -102l), class = c("tbl_df", "tbl", "data.frame"))   ## app.r ##  library(dplyr) library(choroplethr) library(choroplethrmaps) library(lubridate)  server <- function(input, output) {      output$distplot <- renderplot({      df1 <- filter(df, date==as.character(input$year))        p <- state_choropleth(df1,                           title      = "population estimates",                           legend     = "population",                           num_colors = 1     )     print(p)    }) }  ui <- fluidpage(   sidebarlayout(     sidebarpanel(       sliderinput("year", "year:", min = 2000, max = 2001, step=1, value = 2000, sep = "")     ),     mainpanel(plotoutput("distplot"))   ) )  shinyapp(ui = ui, server = server) 

since state_choropleth() returns ggplot object, can use scale_fill_gradient(). can range of data range(df$value).

so if renderplot() returns:

print(p + scale_fill_gradient(limits = range(df$value)) 

it should job.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -