This posts shows how easy it can be to build an visually pleasing plot. We will use hrbrmster’s ggcounty
, which is an R package at this Github repo. Graphics engine is as mostly in my plots, Hadley Wickhams ggplot
. All build on R. Standing on shoulders…
Disclaimer: This example heavily draws on hrbrmster example on this page. All credit is due to Rudy, and those on whose work he built up on.
First, load the relevant packages:
library(ggcounty) # us maps
library(viridis) # color scheme
library(tidyverse) # data handling
## Warning: package 'dplyr' was built under R version 3.5.1
Load population data:
data(population, package = "ggcounty")
Split population figures in bins:
population$brk <- cut(population$count,
breaks=c(0, 100, 1000, 10000, 100000, 1000000, 10000000),
labels=c("0-99", "100-1K", "1K-10K", "10K-100K",
"100K-1M", "1M-10M"),
include.lowest=TRUE)
Get the US counties map:
us <- ggcounty.us()
## Loading required package: sp
## Loading required package: maptools
## Checking rgeos availability: TRUE
## Warning: use rgdal::readOGR or sf::st_read
## Warning: Ignoring unknown aesthetics: x, y
gg <- us$g
us$g
contains base map:
us$g
Add population data, this plot is called choropleth:
gg <- gg + geom_map(data=population, map=us$map,
aes(map_id=FIPS, fill=brk),
color="white", size=0.125)
Now give it the viridis color scheme:
gg + scale_fill_viridis_d()
Besides the default scale of virids, there are some other scales, such as magma
and inferno
:
gg + scale_fill_viridis_d(option = "magma") +
theme(legend.position = "none")
Stunning, isn’t it? And simple. Enjoy!