Visualizing Geographic Data in R

R Packages and Dependencies

library(tidyverse)
library(geofacet)
library(stringr)
library(fiftystater)

What is a data map?

####################################
# chloropleth map of state gdps
####################################
low_color='#ccdbe5'
high_color="#114365"
legend_title = 'GDP ($, trillions)'
ggplot(gdp_area, aes(map_id = state_full)) +
geom_map(aes(fill = gdp ),color="#ffffff",size=.15, map = fifty_states) +
expand_limits(x = fifty_states$long, y = fifty_states$lat) +
coord_map() +
labs(x = "", y = "") +
scale_x_continuous(breaks = NULL) +
scale_y_continuous(breaks = NULL) +
scale_fill_continuous(low = low_color, high= high_color, guide = guide_colorbar(title = legend_title)) + # creates shading pattern
theme(#legend.position = "bottom",
panel.background = element_blank()) +
fifty_states_inset_boxes() +
ggtitle('State GDP 2016', subtitle = 'California had the highest GDP by a wide margin')
####################################
## scatter plot of GDP and land area
####################################
# top states are states which will be annotated
top_states <- gdp_area %>%
filter(gdp > .9 | area > 1 | state == 'IN')
# calculate r2 between variables for scatter plot
r2 = str_c("R-squared = ", format(cor(gdp_area$gdp, gdp_area$area) ** 2, digits=2, nsmall = 2))
# create scatter plot
gdp_area %>%
ggplot() +
geom_point(aes(x=area, y=gdp, color= (area > .5 | gdp > .9 | state=='IN'))) +
scale_color_manual(name = '', values = c('black', '#1f77b4')) +
geom_text(aes(x=area, y=gdp,label=state), color='#1f77b4', data=top_states, vjust=-.4) +
ggtitle('State GDP vs. Land Area', subtitle = 'There is little correlation between GDP and state size' ) +
xlab('Land Area (Sq. Km, millions)') +
ylab('GDP ($, trillions)') +
annotate('text', x=1.4, y=2.65, label=r2) +
scale_y_continuous(limits=c(0,2.75)) +
scale_x_continuous(limits=c(0,1.6)) +
theme(legend.position="none")

Alternatives to the Standard Data Map

####################################
# tile map
####################################
create_gradient_state_tile_map <- function(state, value, title, legend_title, low_color='#ccdbe5', high_color="#114365", state_grid='us_state_grid2') {

df <- as.tibble(data.frame(state, value))

fig <- df %>%
mutate(x = 1) %>% # size of the bar being plotted. All bars should be same size to make perfect squares
mutate(label_y = .5) %>% # this location of state labels
mutate(label_x = 1) %>%
ggplot()+
geom_bar(mapping=aes(x=x, fill=value)) +
facet_geo(~ state, grid=state_grid) +
scale_fill_continuous(low = low_color, high= high_color, guide = guide_colorbar(title = legend_title)) + # creates shading pattern
ggtitle(title) +
theme_classic() + # theme classic removes many ggplot defaults (grey background, etc)
theme(plot.title = element_text(size = 28), # format the plot
plot.margin = unit(c(1,1,1,1), "cm"),
legend.text=element_text(size=16),
legend.title = element_text(size=16),
axis.title=element_blank(),
axis.text=element_blank(),
axis.ticks = element_blank(),
strip.text.x = element_blank(),
axis.line = element_blank()) +
geom_text(aes(x=label_x, y=label_y, label=state), color='#ffffff', size=10)

return(fig)
}
# Using 2016 values, create a tile map
tile_map <- create_gradient_state_tile_map(gdp_area$state, gdp_area$gdp, title='State GDP 2016 \n', legend_title = "GDP, ($, trillions)" )
tile_map
####################################
# simple bar chart of state gdps
####################################
gdp_area %>%
ggplot()+
geom_bar(mapping=aes(x=reorder(state, gdp), y=gdp), stat = 'identity') +
coord_flip() +
scale_y_continuous(expand = c(0,0), limits = c(0,2.75)) +
ylab("GDP ($, trillions)") +
xlab('State') +
ggtitle('State GDP 2016') +
theme(axis.text = element_text(size=6))

What about time-series data?

#########################################
# tile area map
#########################################
# create state tile map
ggplot(df, aes(date, gdp)) +
geom_area(fill='#114365') +
facet_geo(~ state, grid = "us_state_grid2", label="name") +
scale_x_continuous(labels = function(x) paste0("'", substr(x, 3, 4))) +
labs(title = "GDP by State 1997-2016 ($, trillions)",
caption = "Data Source: St. Louis FRED",
x = "Year",
y = "GDP") +
theme(strip.text.x = element_text(size = 8))

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store