This is a practice from the book Data Visualization A Practical Introduction by professor Kieran Healy.
library(maps)
library(ggplot2)
library(ggmap)
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(socviz)
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v tibble 3.1.6 v dplyr 1.0.7
## v tidyr 1.1.4 v stringr 1.4.0
## v readr 2.1.0 v forcats 0.5.1
## v purrr 0.3.4
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
## x purrr::map() masks maps::map()
library(mapproj)
library(mapdata)
us_states <- map_data("state")
p <- ggplot (data = us_states ,
aes(x = long,
y= lat,
group =group,
fill =region))
p + geom_polygon (color = "gray90", size = 0.1) + guides(fill= FALSE)
## Warning: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
## "none")` instead.
election$region <- tolower(election$state)
election$region <- tolower(election$state)
us_states_elec <- left_join(us_states, election)
## Joining, by = "region"
party_colors <- c("#2E74C0","#CB454A")
p0 <- ggplot(data= us_states_elec,
mapping = aes(x =long, y = lat, group= group, fill = party))
p1 <- p0 + geom_polygon(color = "gray90", size = 0.1) + coord_map(projection = "albers", lat0 = 39, lat1 = 45)
p2 <- p1 +scale_fill_manual(values = party_colors) +
labs (title ="Election results 2016", fill =NULL)
p2 + theme(axis.line = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(),
panel.background = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
panel.spacing = unit(0,"line"),
plot.background = element_blank(),
legend.justification = c(0,0),
legend.position = c(0,0))
theme_map <- theme(axis.line = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(),
panel.background = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
panel.spacing = unit(0,"line"),
plot.background = element_blank(),
legend.justification = c(0,0),
legend.position = c(0,0))
p0 <- ggplot(data = us_states_elec,
mapping= aes (x= long, y = lat, group = group, fill = pct_trump))
p1 <- p0 + geom_polygon(color = "gray90", size =0.1)+
coord_map (projection ="albers", lat0= 39, lat1 =45)
p1 + labs (title ="Trump vote") + theme_map + labs(fill = "Percent")
p2 <- p1 + scale_fill_gradient(low = "white", high ="#CB454A") + labs(title ="Trump vote")
p2 + theme_map+ labs(fill= "Percent")
For election result, we might refer a gradient that diverge from a midpoint. The scale_gradient2() function gives us a blue-red spectrum that passes through white by default.
Alternatively, we can re-specify the mid-level color along with the high and low colors. We will make purple our midpoint and use the muted() function from the scales library to tone down the color a little.
p0 <- ggplot(data = us_states_elec,
mapping = aes(x = long, y = lat, group = group, fill = d_points))
p1 <- p0 + geom_polygon(color = "gray90", size = 0.1) +
coord_map(projection = "albers", lat0 =30, lat1= 45)
p2 <- p1 + scale_fill_gradient2() +
labs(title = "Winning margins", fill = "Percent") + theme_map
p2
p3 <- p1 + scale_fill_gradient2(low ="red",
mid = scales::muted("purple"),
high ="blue",
breaks = c(-25, 0, 25, 50, 75))+ labs(title ="Winning margins")
p3 + theme_map + labs(fill= "Percent")