Matt O'Brien February 2016

Fix range across choropleths in Shiny when using choroplethr package

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).

enter image description here

Notice now that California has the exact same dark blue for the second year (corresponding to the value of 500).

enter image description here

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", 
                                "massach        

Answers


alexwhan February 2016

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.

Post Status

Asked in February 2016
Viewed 2,709 times
Voted 9
Answered 1 times

Search




Leave an answer