0

我想复制使用此链接中的确切代码构建的地图 - 仅来自“步骤 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
4

1 回答 1

2

通过检查代码,是呈现choroggplot2对象,因此这是ggplot2添加更多元素的地方,而不是zip_map$ggplot_polygon只接受geom_polygon分配的对象。

# 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')

在此处输入图像描述

于 2017-10-22T22:15:20.820 回答