使用 brewer 调色板自定义 ggplot2 geom_polygon 中的颜色
Customize colors in ggplot2's geom_polygon using brewer palette
我想复制一个地图,该地图使用来自此 link 的确切代码构建 - 仅来自 "Step 1, Building the Initial Map":
https://www.r-bloggers.com/user-question-how-to-add-a-state-border-to-a-zip-code-map/
但是,我希望颜色不是蓝色而是 else.So,我只在代码中添加了一行 - 在 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
通过检查代码,choro
是渲染的 ggplot2
对象,因此这是进一步添加 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')
我想复制一个地图,该地图使用来自此 link 的确切代码构建 - 仅来自 "Step 1, Building the Initial Map":
https://www.r-bloggers.com/user-question-how-to-add-a-state-border-to-a-zip-code-map/
但是,我希望颜色不是蓝色而是 else.So,我只在代码中添加了一行 - 在 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
通过检查代码,choro
是渲染的 ggplot2
对象,因此这是进一步添加 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')