提问人:Sean Wallace 提问时间:6/14/2022 最后编辑:Sean Wallace 更新时间:6/19/2022 访问量:114
尝试向 choroplethr 县地图添加边框颜色时未找到对象“value”
Object 'value' not found when trying to add border color to choroplethr county map
问:
我想在我为新泽西州创建的县分区统计图中添加黑色边框。这是我当前的代码:
county_choropleth(tni_data,
state_zoom = "new jersey",
reference_map = TRUE,
num_colors = 8) +
scale_fill_brewer(palette="YlOrRd") +
labs(title = "DV/SV Incidents by New Jersey County",
legend = "DV/SV Incidents (total)") +
geom_polygon(aes(fill=value), color="black")
基于前面的这个问题。
我收到此错误:
Error in FUN(X[[i]], ...) : object 'value' not found
任何关于可能出错的建议将不胜感激!
更新:包括最小的可重复示例!
tni_data <- structure(list(county = c("atlantic", "bergen", "burlington",
"camden", "cape may", "cumberland", "essex", "gloucester", "hudson",
"hunterdon", "mercer", "middlesex", "monmouth", "morris", "ocean",
"passaic", "salem", "somerset", "sussex", "union", "warren"),
value = c(3187, 3410, 3747, 6532, 1339, 2510, 6420, 2417,
2656, 570, 2139, 4336, 3514, 1802, 4372, 2451, 773, 1840,
1265, 3054, 1311), region = c(34001, 34003, 34005, 34007,
34009, 34011, 34013, 34015, 34017, 34019, 34021, 34023, 34025,
34027, 34029, 34031, 34033, 34035, 34037, 34039, 34041),
county.fips.character = c("34001", "34003", "34005", "34007",
"34009", "34011", "34013", "34015", "34017", "34019", "34021",
"34023", "34025", "34027", "34029", "34031", "34033", "34035",
"34037", "34039", "34041"), state.name = c("new jersey",
"new jersey", "new jersey", "new jersey", "new jersey", "new jersey",
"new jersey", "new jersey", "new jersey", "new jersey", "new jersey",
"new jersey", "new jersey", "new jersey", "new jersey", "new jersey",
"new jersey", "new jersey", "new jersey", "new jersey", "new jersey"
), state.fips.character = c("34", "34", "34", "34", "34",
"34", "34", "34", "34", "34", "34", "34", "34", "34", "34",
"34", "34", "34", "34", "34", "34"), state.abb = c("NJ",
"NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ",
"NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ"
), tni.per.capita = c(0.0120870785451511, 0.00365800545375359,
0.00841362616734292, 0.0128970859140997, 0.0145481806625452,
0.0167862660255339, 0.00803529522200319, 0.00828772853831489,
0.00395008261562097, 0.00458306196782208, 0.00582151702365076,
0.00525536262729346, 0.00567877891708886, 0.00366375585804471,
0.00720042952241982, 0.00488416303659037, 0.0123907990702893,
0.00559382733314282, 0.00900432777176698, 0.00548943903109783,
0.0124540454273419)), class = "data.frame", row.names = c(NA,
-21L))
county_choropleth(tni_data,
state_zoom = "new jersey",
reference_map = TRUE,
num_colors = 8) +
scale_fill_brewer(palette="YlOrRd") +
labs(title = "DV/SV Incidents by New Jersey County",
legend = "DV/SV Incidents (total)") +
geom_polygon(aes(fill=value), color="black")
答:
您是否定义了“价值”一词?对于颜色/填充,您需要有一个分类列,其中包含颜色组所基于的内容。
因此,对于他们链接的示例,他们定义为 .但那是在多边形中着色。fakedata$value
runif(n=56, min=0, max=1)
如果你只是想要一个黑色边框,我认为应该这样做吗?如果不了解tni_data的数据是什么样子,就很难分辨。geom_polygon(aes(color = "black"))
评论
这是一个最小的可重复示例,我相信它显示了您的情况:
library(choroplethr)
library(choroplethrMaps)
# draw map to show "problem" - no borders on counties
county_choropleth(df_pop_county, state_zoom = "new jersey")
从您下面的评论来看,听起来您想为这张地图添加彩色边框。
关键点是:所有等值均衡函数都返回 ggplot2 对象。因此,虽然没有参数可以county_choropleth,但这实际上不是问题。我们可以通过编写如下代码来自己添加该边框:set_border_color
county_choropleth(...) + geom_polygon(...)
复杂的是,新层 () 需要自己的数据。这些数据需要是新泽西州各县的地图。获取该地图的过程分为两个步骤:geom_polygon
1. 获取 NJ FIPS 代码(又名“区域”)的向量
您可能不知道这一点,但 choroplethrMaps 附带了一个名为 dataframe,其中包含美国每个县的 FIPS 代码。我们将使用它来获取新泽西州每个县的FIPS代码。county.regions
library(tidyverse)
data(county.regions)
nj_regions = filter(county.regions, state.name == "new jersey") %>% pull(region)
nj_regions
请注意,choroplethr 之所以调用这些术语,是因为它是一个中性术语,可用于多个地理位置。也就是说,无论是处理州、县、国家还是邮政编码,我们始终可以使用“地区”一词。regions
2. 将县地图子选为新泽西州
您可能不知道这一点,但 choroplethrMaps 附带了它用于渲染县的实际县地图。它被称为 .现在我们有了要子选的区域(即 FIPS 代码),我们可以这样做:county.map
data(county.map)
nj.county.map = filter(county.map, region %in% nj_regions)
3. 将边框添加为新图层
现在,我们可以将新泽西州县地图添加为具有自己颜色的边框。请注意,我们在这里使用来确保此图层呈现为边框(而不是填充)。在这里,我使用黄色,但显然您可以根据需要更改它:fill = NA
county_choropleth(df_pop_county, state_zoom = "new jersey") +
geom_polygon(data = nj.county.map,
aes(long, lat, group = group),
color = "yellow",
fill = NA,
size = 0.2)
仅供参考,我创建了三门关于choroplethr的全长课程(见这里)。现在全部免费。他们也可能对你有所帮助。
我一直想创建第四个来处理这样的问题:混合 choroplethr 和 ggplot2 以创建自定义地图。像你这样的问题已经出现了很多。我在这里给出如此详细答案的一个原因是,我认为我永远不会创建这样的课程。但希望这个答案能帮助你和下一个有这个问题的人。这些解决方案的结构往往是相同的:使用辅助数据结构(例如 和 ),然后添加一个新的 ggplot2 层。county.map
county.regions
评论
<region>_choropleth
<region>.map
<region>.regions
评论