我已经将geom_map与map_data一起使用,但不包括HI和AK。我使用美国各州的美国人口普查地理边界数据与geom_polygon和state.center但它们不匹配。我已经阅读了stackoverflow上的相关帖子,但我读过的那些帖子并没有真正回答我的问题。有什么建议吗?
答案 0 :(得分:2)
library(choroplethr)
library(ggplot2)
library(devtools)
install_github('arilamstein/choroplethrZip@v1.3.0')
library(choroplethrZip)
data(df_zip_demographics)
df_zip_demographics$value = df_zip_demographics$percent_asian
zip_map = ZipChoropleth$new(df_zip_demographics)
zip_map$ggplot_polygon = geom_polygon(aes(fill = value),
color = NA)
zip_map$set_zoom_zip(state_zoom = NULL,
county_zoom = NULL,
msa_zoom = NULL,
zip_zoom = NULL)
zip_map$title = "50 State Map for StackOverflow"
zip_map$legend = "Asians"
zip_map$set_num_colors(4)
choro = zip_map$render()
choro
data(df_pop_state)
outline = StateChoropleth$new(df_pop_state)
outline = outline$render_state_outline(tolower(state.name))
choro_with_outline = choro + outline
choro_with_outline
此外,以下是添加州标签的代码:https://github.com/arilamstein/choroplethr/blob/master/R/state.R#L33
答案 1 :(得分:2)
您需要重新考虑一下您的策略,因为您需要移动一些州标签以提高可读性。 Prbly shld也会从状态到你移动的标签画一条线。
library(ggplot2)
library(ggalt) # coord_proj
library(albersusa) # devtools::install_github("hrbrmstr/albersusa")
library(ggthemes) # theme_map
library(rgeos) # centroids
library(dplyr)
# composite map with AK & HI
usa_map <- usa_composite()
# calculate the centroids for each state
gCentroid(usa_map, byid=TRUE) %>%
as.data.frame() %>%
mutate(state=usa_map@data$iso_3166_2) -> centroids
# make it usable in ggplot2
usa_map <- fortify(usa_map)
gg <- ggplot()
gg <- gg + geom_map(data=usa_map, map=usa_map,
aes(long, lat, map_id=id),
color="#2b2b2b", size=0.1, fill=NA)
gg <- gg + geom_text(data=centroids, aes(x, y, label=state), size=2)
gg <- gg + coord_proj(us_laea_proj)
gg <- gg + theme_map()
gg