【问题标题】:Change colours of filling and not just borders in geom_sf()更改填充颜色,而不仅仅是 geom_sf() 中的边框
【发布时间】:2021-04-04 16:23:17
【问题描述】:

我正在绘制一个 sf 对象(多面体)。根据cand_dummy 列中每个观察值的值,它的几何图形填充了颜色。然而,我试图改变这个情节中的很多东西,但要么我得到了带有颜色的几何图形,但我不能改变颜色,就像这样:

library(ggplot2)
 ggplot(data = dta_plot) +
  geom_sf(data = dta_plot, aes(fill = cand_dummy))+
  theme_map()+
  coord_sf(expand = FALSE)+
  theme(legend.position="top")+
  theme(legend.position="top")+
  scale_fill_discrete(name = "Cand", labels = c("No", "Yes"))+
  scale_fill_manual(values = c("white", "green4"))

这将为cand_dummy 的不同值打印不同的颜色,但不是我在scale_fill_manual 上设置的颜色。 或者,我在aes 中将fill 更改为colour 并执行以下操作:

ggplot(data = dta_plot) +
  geom_sf(data = dta_plot, aes(colour = cand_dummy))+
  theme_map()+
  coord_sf(expand = FALSE)+
  theme(legend.position="top")+
  theme(legend.position="top")+
  scale_fill_discrete(name = "Cand", labels = c("No", "Yes"))+
  scale_fill_manual(values = c("white", "green4"))

但是现在我用这些颜色获得了每个几何图形的边界/边界,而几何图形本身被着色为灰色。所以我找不到获得我想要的东西的方法。 任何的想法? 谢谢!

【问题讨论】:

    标签: r ggplot2 plot gis sf


    【解决方案1】:

    我认为问题在于您在代码中同时包含了 scale_fill_manual 和 scale_fill_discrete 函数。我相信你应该只有以下功能:

    scale_fill_manual(values = c("white", "green4"), name = "Cand", labels = c("No", "Yes"))
    

    如果这不适用于您的第一个代码块,请告诉我。

    【讨论】:

    • 谢谢!它成功了!我能问一下为什么会这样吗?因为如果最后一个 scale_fill 覆盖了另一个,我会在我的问题中提出的示例中获得我想要的颜色。
    • 抱歉,我没有对此进行讨论,因为我对此并不完全清楚。我对文档的理解是 scale_fill_discrete 本质上不是您可以添加的函数,因为它更多的是在幕后用于创建初始填充的函数,因此不接受指定的参数。 scale_fill_manual() 因此无法与此功能一起使用。试试这里的文档。 ggplot2.tidyverse.org/reference/scale_colour_discrete.html其他人可能有更好或更明确的理由。
    • 不,太好了,您不必讨论,只是我没有真正理解问题所在。现在很清楚了。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2019-03-09
    • 2021-09-03
    • 2013-12-15
    • 1970-01-01
    • 2021-05-23
    • 1970-01-01
    • 2013-07-23
    • 1970-01-01
    相关资源
    最近更新 更多