【问题标题】:ggplot and geom_sf and Error: length(rows) == 1 is not TRUEggplot and geom_sf and Error: length(rows) == 1 is not TRUE
【发布时间】:2018-07-30 03:34:25
【问题描述】:

我正在尝试使用ggplot2 和新的(非常棒的)简单特征映射包geom_sf 将几个地理数据集绘制在一起。当我尝试强制 line 类型特征的图例显示为行。

这是我调用的生成下图的代码;一切都很好除了图例,其中 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)])

这是图表:

现在,如果我尝试使用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。我已经尝试了几天才能找到答案,但没有运气。

【问题讨论】:

    标签: r ggplot2 legend sf


    【解决方案1】:

    我现在看不到解决方案,但至少这里有一个可重现的最小示例。
    也许这是一个错误,那么最好的选择是在 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
    

    注: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") 
    

    reprex package (v0.2.0) 于 2018 年 2 月 20 日创建。

    【讨论】:

    • 太好了——感谢您开发和发布 MRE。我得到了完全相同的模式,单独一切都很好,直到出现错误。有意思的是最后一个框没有报错。
    • 当您说 post to github 时,您是指 sf https://github.com/r-spatial/sf 的那个吗?
    • 我的意思是 ggplot 的 github 问题,因为它更有可能是 ggplot 问题。也许在打扰 ggplot 开发人员之前,最好稍等片刻,看看这里是否有人有解决方案。我已经在代表中添加了数字。
    • 谢谢@Gilles,我会这样做的。
    【解决方案2】:

    这是由于处理 ggplot2 中缺失的美学,自开发以来刚刚解决。版本包版本2.2.1.9000:https://github.com/tidyverse/ggplot2/commit/4635bbb1e50d94e8d2017f084f75e2f709daeb18

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-27
      • 2012-07-18
      • 1970-01-01
      • 1970-01-01
      • 2018-03-28
      • 2015-05-20
      相关资源
      最近更新 更多