我想复制使用此链接中的确切代码构建的地图 - 仅从“步骤1,构建初始地图”:
https://www.r-bloggers.com/user-question-how-to-add-a-state-border-to-a-zip-code-map/
但是,我希望颜色不是蓝色,而是其他东西。所以,我只在代码中添加了一行 - 就在ggplot_polygon
之后:
+scale_fill_brewer(palette = 'OrRd')
但它不起作用。知道为什么吗?
以下是我的代码:
library(choroplethr)
library(ggplot2)
library(devtools)
install_github('arilamstein/choroplethrZip@v1.3.0')
library(choroplethrZip)
# load the data
data(df_zip_demographics)
str(df_zip_demographics)
df_zip_demographics$value = df_zip_demographics$percent_white
# create the map
zip_map = ZipChoropleth$new(df_zip_demographics)
zip_map$ggplot_polygon = geom_polygon(aes(fill = value),
color = NA) +
scale_fill_brewer(palette = 'OrRd')
zip_map$set_zoom_zip(state_zoom = c("new york", "new jersey"),
county_zoom = NULL,
msa_zoom = NULL,
zip_zoom = NULL)
zip_map$title = "New York and New Jersey ZCTAs"
zip_map$legend = "Percent White"
zip_map$set_num_colors(4)
choro = zip_map$render()
choro
答案 0 :(得分:2)
通过检查代码,choro
是呈现的ggplot2
对象,因此这是添加ggplot2
元素的地方,而不是仅zip_map$ggplot_polygon
geom_polygon
1}}任务。
# create the map
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 = c("new york", "new jersey"),
county_zoom = NULL,
msa_zoom = NULL,
zip_zoom = NULL)
zip_map$title = "New York and New Jersey ZCTAs"
zip_map$legend = "Percent White"
zip_map$set_num_colors(4)
choro = zip_map$render()
choro
# Change fill to brewer palette
choro + scale_fill_brewer(palette = 'OrRd')