Reputation: 1299
I've been learning how to build maps for work showing covid-19 infection data. In addition to a national map, I am producing regional maps for the northeast, south, west, and midwest. The code is identical, I just use different filterings on the national data frame. I produce one map for each day of data and use gganimate to make frames and finally gifski to make an animated gif. The basic code for the national map is:
p <- pop_counties_cov %>%
ggplot() +
geom_sf(mapping = aes(fill = infRate, geometry=geometry), color = NA) +
geom_sf(data = states_sf, fill = NA, color = "black", size = 0.25) +
coord_sf(datum = NA) +
scale_fill_gradient(name = "% Population Infected", trans = "log", low='green', high='red',
na.value = "white",
breaks=c(0, round(max(pop_counties_cov$infRate),3))) +
geom_point(data=AFMCbases, aes(x=longitude.1, y=latitude.1,size=personnel), color = "hotpink") +
#geom_label_repel(data=AFMCbases, aes(x=longitude.1, y=latitude.1, label=Base)) +
theme_bw() +
labs(size='AFMC \nMil + Civ') +
theme(legend.position="bottom",
panel.border = element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank())
As an example, the final frame it produces is
This code on the other hand:
p <- mw_pop_counties_cov %>%
ggplot() +
geom_sf(mapping = aes(fill = infRate, geometry=geometry), color = NA) +
geom_sf(data = mw_states_sf, fill = NA, color = "black", size = 0.25) +
coord_sf(datum = NA) +
scale_fill_gradient(name = "% Population Infected", trans = "log", low='green', high='red',
na.value = "white",
breaks=c(0, round(max(mw_pop_counties_cov$infRate),3))) +
geom_point(data=mwBases, aes(x=longitude.1, y=latitude.1,size=personnel), color = "hotpink") +
#geom_label_repel(data=AFMCbases, aes(x=longitude.1, y=latitude.1, label=Base)) +
theme_bw() +
labs(size='AFMC \nMil + Civ') +
theme(legend.position="bottom",
panel.border = element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank())
which is the same except that the data frames have been filtered down to only the midwest states, produces
Note the appearance of the color scale.
Is there some typo I'm not seeing because I've been staring at this too long?
My script produces 5 animations, 1 each for the national map, then the 4 census regions (northeast, midwest, south and west). The color scale appears on the west and midwest maps, but not the other three. This is despite the fact I basically just cut and pasted and then changed the dataframe names.
What am I doing wrong? I WANT the color scale to appear on ALL maps.
Upvotes: 3
Views: 620
Reputation: 1299
Turns out the issue does appear to be one of horizontal space on the output plot. By simply changing the scale caption from
scale_fill_gradient(name = "% Population Infected",...
to
scale_fill_gradient(name = "% Population \nInfected",
Note the newline character \n
The output map now has the color scale I was looking for
Upvotes: 1