Lets map the life expectancy in White and African-American in US.
Import the data from Wikipedia and do some data cleaning.
myData = read_html("https://en.wikipedia.org/wiki/List_of_U.S._states_by_life_expectancy")
myData = myData %>% html_nodes("table") %>% .[[2]] %>% html_table(fill=T)
myData = myData[c(1:8)]
names(myData) = myData[3,]
myData = myData[-c(1:3), ]
myData = myData[, -c(5:7)]
names(myData)[c(4,5)] = c("le_black", "le_white")
myData = myData %>% mutate(le_black = as.numeric(le_black), le_white = as.numeric(le_white))
Since there are some differences in life expectancy between White and African-American, calculate the differences and map it.
myData = myData %>% mutate(le_diff = (le_white - le_black))
Load the map data and will merge the datasets.
states = map_data("state")
myData$region = tolower(myData$State)
# merge the datasets
states = merge(states, myData, by="region", all.x=T)
When there is no data, color it in grey.
ggplot(states, aes(x = long, y = lat, group = group, fill = le_black)) + geom_polygon(color = "white") +
scale_fill_gradient(name = "Years", low = "#ffe8ee", high = "#c81f49", guide = "colorbar", na.value="#eeeeee", breaks = pretty_breaks(n = 5)) + labs(title="Life expectancy in African American") + coord_map()
ggplot(states, aes(x = long, y = lat, group = group, fill = le_white)) + geom_polygon(color = "white") +
scale_fill_gradient(name = "Years", low = "#ffe8ee", high = "#c81f49", guide = "colorbar", na.value="Gray",
breaks = pretty_breaks(n = 5)) + labs(title="Life expectancy in White") + coord_map()
Illustrate the differences between White and African American life expectancy.
ggplot(states, aes(x = long, y = lat, group = group, fill = le_diff)) + geom_polygon(color = "white") +
scale_fill_gradient(name = "Years", low = "#ffe8ee", high = "#c81f49", guide = "colorbar", na.value="#eeeeee",
breaks = pretty_breaks(n = 5)) +
labs(title="Differences in Life Expectancy between \nWhite and African Americans") + coord_map()
Add some interactivity using Plotly:
library(plotly)
map_plot = ggplot(states, aes(x = long, y = lat, group = group, fill = le_black)) + geom_polygon(color = "white") +
scale_fill_gradient(name = "Years", low = "#ffe8ee", high = "#c81f49", guide = "colorbar", na.value="#eeeeee", breaks = pretty_breaks(n = 5)) + labs(title="Life expectancy in African American") + coord_map()
ggplotly(map_plot)