gpt4 book ai didi

r - ggplot 和 geom_sf 以及错误 : length(rows) == 1 is not TRUE

转载 作者:行者123 更新时间:2023-12-03 22:36:56 25 4
gpt4 key购买 nike

我正在尝试使用 ggplot2 将多个地理数据集绘制在一起以及新的(很棒的)简单功能映射包 geom_sf .我对传说和错误代码感到困惑 Error: length(rows) == 1 is not TRUE (似乎属于 ddply )当我尝试强制使用 的图例时产生的结果线路键入功能以显示为一条线。

这是我调用的生成下图的代码;一切都很好,除了图例,它的 Line1 显示有一个框/填充。

ggplot() + 
geom_sf(data=sct, aes(fill=as.factor(sct$tc2)), color = "gray82") +
scale_fill_manual(values=c("white","goldenrod1","dodgerblue"),
labels = c("Omitted", "Control", "Treated"),
name = "Legend") +
geom_sf(data=lines1925All, aes(color="A"), linetype="dashed") +
scale_color_manual(values = c("A" = "olivedrab"),
labels = c("Line1"),
name = "what line?") +
theme_minimal() +
coord_sf(xlim=mapRange2[c(1:2)], ylim=mapRange2[c(3:4)])

这是图表:

enter image description here

现在,如果我尝试使用 show.legend 强制图例看起来像一条线( = TRUE= "line" ),在下面
  geom_sf(data=lines1925All, aes(color="A"), linetype="dashed", show.legend = "line") +

我收到错误 Error: length(rows) == 1 is not TRUE .如果我绘制 geom_sf 的任一实例独自一人,我没有错误,并且可以通过使用 show.legend = "line" 使图例看起来正确.

注意:我没有包含一个最小的可重复示例,因为我无法通过易于共享的数据复制它;见 Add multiple legends to ggplot2 when using geom_sf .我已经尝试了几天来找到答案,但没有运气。

最佳答案

我现在没有看到解决方案,但至少这是一个最小的可重现示例。
也许这是一个错误,那么最好的选择是在 github 上发布问题。

library(sf)
#> Linking to GEOS 3.5.1, GDAL 2.1.3, proj.4 4.9.2, lwgeom 2.3.2 r15302
library(ggplot2)

# Create a ploygon object (with 3 polygons)
poly1 <- cbind(lon=c(5, 6, 7, 5),
lat=c(52, 53, 51, 52))
poly <- st_sf(st_sfc(list(st_polygon(list(poly1)),
st_polygon(list(poly1 - 1)),
st_polygon(list(poly1 + 1)))))
poly$treatment <- factor(c("Omitted", "Control", "Treated"))

# create two line objects
line1 <- st_sf(st_sfc(list(st_linestring(cbind(lon = c(5.5, 4.5), lat = c(53.5, 54.5))))))
line2 <- st_sf(st_sfc(list(st_linestring(cbind(lon = c(5, 7.5), lat = c(53, 52))))))

这有效:

ggplot() + 
geom_sf(data= line1, aes(color="A"), show.legend = "line")



这也有效

ggplot() + 
geom_sf(data= line1, aes(color="A"), show.legend = "line") +
geom_sf(data = poly)



这有效,但我们希望颜色图例显示一条线

ggplot() + 
geom_sf(data= line1, aes(color="A")) +
geom_sf(data = poly, aes(fill = treatment))



这不起作用

ggplot() + 
geom_sf(data= line1, aes(color="A"), show.legend = "line") +
geom_sf(data = poly, aes(fill = treatment))
#> Error: length(rows) == 1 n'est pas TRUE

Nb : color = "A" 的奇怪用法在审美上是因为
对于空间数据,我们通常需要不同的空间数据集
设置美学,但我们也想要一个传奇(例如:蓝色河流
和红色的道路)。
例如 :

ggplot() + 
geom_sf(data= line1, aes(color="A"), show.legend = "line") +
geom_sf(data= line2, aes(color="B"), show.legend = "line") +
geom_sf(data = poly) +
scale_color_manual(values = c("A" = "red", "B" = "blue"),
labels = c("Roads", "Rivers"),
name = "Linear \ncomponents")



创建于 2018-02-20 由 reprex package (v0.2.0)。

关于r - ggplot 和 geom_sf 以及错误 : length(rows) == 1 is not TRUE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48873972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com