【问题标题】:Mix colour, fill and linetype in R ggplot legend在 R ggplot 图例中混合颜色、填充和线型
【发布时间】:2021-06-09 17:02:21
【问题描述】:

我有一张地图,我试图将其添加到具有 2 个填充多边形和 1 个多边形的图例中,但只有轮廓轮廓(仅由虚线表示)。

library(sf);library(ggplot)
# point
p <- rbind(c(1,2), c(2,1), c(3,2), c(3.5,3.5), c(3.4,3.6), c(3.9,1.4))
(mp <- st_multipoint(p))

p1 <- rbind(c(0,0), c(1,0), c(3,2), c(2,4), c(1,4), c(0,0))
p2 <- rbind(c(1,1), c(1,2), c(2,2), c(1,1))
pol <-st_polygon(list(p1,p2))

p3 <- rbind(c(3,0), c(4,0), c(4,1), c(3,1), c(3,0))
p4 <- rbind(c(3.3,0.3), c(3.8,0.3), c(3.8,0.8), c(3.3,0.8), c(3.3,0.3))[5:1,]
p5 <- rbind(c(3,3), c(4,2), c(4,3), c(3,3))
(mpol1 <- st_multipolygon(list(list(p3))))
(mpol2 <- st_multipolygon(list(list(p4))))
(mpol3 <- st_multipolygon(list(list(p5))))

ggplot(mp, aes(geometry = geometry)) + 
  geom_sf(data = pol, aes(geometry = geometry), inherit.aes = FALSE) +
  # Add the points 
  geom_sf(data = mpol1, alpha = 0.5, aes(geometry = geometry, colour = "grey90", fill  = "grey90"), size = 0.05) + 
  geom_sf(data = mpol2,   alpha = 0.5, aes(geometry = geometry, colour = "grey20", fill  = "grey20"), size = 0.05) +
  geom_sf(data = mpol3, alpha = 0.5, aes(geometry = geometry,colour = "grey30", fill=NA), size = 0.8, linetype = "dotted") +
  scale_color_manual(values = c(alpha("grey90",.5),alpha("grey20",.5),alpha("grey30",.5)), labels = c("item1", "item2","item3"), name="My Leg. hurts") +
  scale_fill_manual( values = c(alpha("grey90",.5),alpha("grey20",.5),NA), labels = c("item1", "item2","item3"), name="My Leg. hurts") + 
  theme_bw()+
  theme(legend.key = element_rect(fill = NA), 
        legend.background = element_rect(fill = NA),
        legend.text = element_text(size = 12), 
        legend.title = element_text(size = 12,face='bold'))

给予

但我想要这个:

如何做到这一点?

【问题讨论】:

    标签: r ggplot2 legend sf


    【解决方案1】:

    详细说明 Ian Campbell 之前的回答:

    考虑这段代码(我唯一的补充是在最后一次geom_sf() 调用中的show.legend = 'line'

    ggplot(mp, aes(geometry = geometry)) + 
      geom_sf(data = pol, aes(geometry = geometry), inherit.aes = FALSE) +
      # Add the points 
      geom_sf(data = mpol1, alpha = 0.5, aes(geometry = geometry, colour = "grey90", fill  = "grey90"), size = 0.05) + 
      geom_sf(data = mpol2,   alpha = 0.5, aes(geometry = geometry, colour = "grey20", fill  = "grey20"), size = 0.05) +
      geom_sf(data = mpol3, alpha = 0.5, aes(geometry = geometry,colour = "grey30", fill=NA), size = 0.8, linetype = "dotted", show.legend = 'line') +
      scale_color_manual(values = c(alpha("grey90",.5),alpha("grey20",.5),alpha("grey30",.5)), labels = c("item1", "item2","item3")) +
      scale_fill_manual( values = c(alpha("grey90",.5),alpha("grey20",.5),NA), labels = c("item1", "item2","item3"), guide = FALSE) + 
      theme_bw()+ 
      theme(legend.key = element_rect(fill = NA), 
            legend.background = element_rect(fill = NA),
            legend.text = element_text(size = 12), 
            legend.title = element_text(size = 12,face='bold')) +
      guides(color = guide_legend(override.aes = 
                                    list(color=c(NA,NA,"grey20"), 
                                         fill=c("grey90","grey20","white"),
                                         linetype = c("dotted")))) +
      labs(color = "My Leg. hurts")
    

    【讨论】:

    • 我以前用过show.legend = 'none',但没有意识到你可以做到这一点。整洁。
    • 谢谢!有时这是必要的——geom_sf() 的同一个调用在不同的上下文(点、线、多边形)中可能意味着不同的东西,有时你需要稍微调整一下图例......
    • 是的!您能否更改 item3 的 outline,以便您看到虚线?
    • 由于某种原因theme(legend.key = element_rect(fill = NA, color = NA)) 不起作用
    • @M.Beausoleil 恐怕我不知道如何处理 :( 你可以在你的指南调用中从 override.aes 中删除linetype = c("dotted"),这将使它 稍微更可口,但盒子周围会留下一条细线。我尝试时无法让它消失
    【解决方案2】:

    这不完全符合您的要求,但非常接近。您可以使用guidesoverride.aes 参数:

    ggplot(mp, aes(geometry = geometry)) + 
      geom_sf(data = pol, aes(geometry = geometry), inherit.aes = FALSE) +
      # Add the points 
      geom_sf(data = mpol1, alpha = 0.5, aes(geometry = geometry, colour = "grey90", fill  = "grey90"), size = 0.05) + 
      geom_sf(data = mpol2,   alpha = 0.5, aes(geometry = geometry, colour = "grey20", fill  = "grey20"), size = 0.05) +
      geom_sf(data = mpol3, alpha = 0.5, aes(geometry = geometry,colour = "grey30", fill=NA), size = 0.8, linetype = "dotted") +
      scale_color_manual(values = c(alpha("grey90",.5),alpha("grey20",.5),alpha("grey30",.5)), labels = c("item1", "item2","item3")) +
      scale_fill_manual( values = c(alpha("grey90",.5),alpha("grey20",.5),NA), labels = c("item1", "item2","item3"), guide = FALSE) + 
      theme_bw()+ 
      theme(legend.key = element_rect(fill = NA), 
            legend.background = element_rect(fill = NA),
            legend.text = element_text(size = 12), 
            legend.title = element_text(size = 12,face='bold')) +
      guides(color = guide_legend(override.aes = 
                                    list(color=c(NA,NA,"grey20"), 
                                         fill=c("grey90","grey20","white"),
                                         linetype = c("dotted")))) +
      labs(color = "My Leg. hurts")
    

    很遗憾,我没有看到更改图例中填充和颜色的 alpha 的好方法。

    【讨论】:

      猜你喜欢
      • 2022-11-22
      • 1970-01-01
      • 2019-11-12
      • 1970-01-01
      • 2019-03-04
      • 2012-12-17
      • 1970-01-01
      • 1970-01-01
      • 2020-10-03
      相关资源
      最近更新 更多