I have a very simple Shiny app, with code at the bottom of the question.
The app allows us to look at the years 2000, and 2001. In both cases, California is the darkest state, since it has the highest values (500 and 1000, respectively).
My issue is that I would like to set the scale for colors to be fixed across both years. Notice that California has a dark blue for the first year (corresponding to the value of 1000).
Notice now that California has the exact same dark blue for the second year (corresponding to the value of 500).
When looking at the choropleths as they stand, it is easy to miss the fact that the value dropped in half across years (and similarly this occurs in different ways for the other states, of course). I would like a way to fix the range across plots. How can I 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 a ggplot
object, you can use scale_fill_gradient()
. You can get the range of all the data with range(df$value)
.
So if renderPlot()
returns:
print(p + scale_fill_gradient(limits = range(df$value))
it should do the job.