使用County_ChoroplethR包为县级地图添加变量值和县名可以通过以下步骤完成:
install.packages("devtools")
devtools::install_github("jaspell/CountyChoropleth")
library(CountyChoropleth)
library(maps)
library(ggplot2)
wages_by_county <- read.csv("wages_by_county.csv", stringsAsFactors=FALSE)
head(wages_by_county)
counties <- map_data("county")
wages_by_county$County <- stringr::str_to_title(wages_by_county$County)
merged_data <- merge(counties, wages_by_county, by.x="subregion", by.y="County" )
county_choropleth(merged_data,
title="Wages by County",
legend="Wages",
num_colors=7,
map="county")
最终的图形输出为一幅以县为单位的彩色区域地图,其中每个县代表一个颜色,颜色的深浅表示该县的平均工资水平高低。每个县名旁边还有相应的数值标记该县的平均工资水平。