This is an example of data plotting on a map with ggplot2. We will display the population and the unemployment rate in the 67 counties of Alabama.
library(dplyr)
library(ggplot2)
library(maps)
library(RColorBrewer)
We refer to the unemp dataset in the maps package.
data(unemp)
str(unemp)
## 'data.frame': 3218 obs. of 3 variables:
## $ fips : int 1001 1003 1005 1007 1009 1011 1013 1015 1017 1019 ...
## $ pop : int 23288 81706 9703 8475 25306 3527 8884 52055 13726 11583 ...
## $ unemp: num 9.7 9.1 13.4 12.1 9.9 16.4 16.7 10.8 18.6 11.8 ...
We join in a new data.frame the 2 datasets county.fips and unemp
data(county.fips)
state <- sub(",(.)*", "", x = county.fips$polyname)
county <- sub("(.)*,", "", x = county.fips$polyname)
cfips <- data.frame(county.fips, state, county)
mydf <- inner_join(cfips, unemp, by = "fips")
We focus on the State of Alabama:
mymap <- map_data("county")
mymap <- mymap[mymap$region == "alabama", ]
# rename the field subregion as region
names(mymap)[5:6] <- c("state", "region")
# variable Population
mydata <- mydf[mydf$state == "alabama", ]
b <- 7 # the number of wanted breaks
qnt <- quantile(mydata$pop, probs = seq(0, 1, 1/b))
mydata$value1 <- cut(mydata$pop, breaks = qnt, include.lowest = TRUE)
sort(unique(mydata$value1))
## [1] [3.25e+03,5.48e+03] (5.48e+03,8.33e+03] (8.33e+03,1.17e+04]
## [4] (1.17e+04,1.84e+04] (1.84e+04,3.03e+04] (3.03e+04,4.87e+04]
## [7] (4.87e+04,3e+05]
## 7 Levels: [3.25e+03,5.48e+03] (5.48e+03,8.33e+03] ... (4.87e+04,3e+05]
# variable Unemployment rate
qnt <- quantile(mydata$unemp, probs = seq(0, 1, 1/b))
mydata$value2 <- cut(mydata$unemp, breaks = qnt, include.lowest = TRUE)
sort(unique(mydata$value2))
## [1] [7.5,9.24] (9.24,10.1] (10.1,11.2] (11.2,12.7] (12.7,14] (14,16.6]
## [7] (16.6,25.6]
## 7 Levels: [7.5,9.24] (9.24,10.1] (10.1,11.2] (11.2,12.7] ... (16.6,25.6]
The following plot displays the population values per county in Alabama:
m <- ggplot(data = mydata, aes(fill = value1,
colour = I("navyblue")))
m <- m + scale_fill_brewer(palette = "YlOrRd")
m <- m + geom_map(aes(map_id = county), map = mymap)
m <- m + expand_limits(x = mymap$long, y = mymap$lat)
m <- m + coord_fixed(ratio = 1)
m <- m + labs(title = "Alabama", x = "", y = "", fill = "Population")
m
The next plot displays the unemployment rates by county in Alabama:
m <- ggplot(data = mydata, aes(fill = value2,
colour = I("red")))
m <- m + scale_fill_brewer(palette = "YlGnBu")
m <- m + geom_map(aes(map_id = county), map = mymap)
m <- m + expand_limits(x = mymap$long, y = mymap$lat)
m <- m + coord_fixed(ratio = 1)
m <- m + labs(title = "Alabama", x = "", y = "", fill = "Unemployment\nrate")
m