在 geom_sf() 中更改填充颜色而不仅仅是边框

Change colours of filling and not just borders in geom_sf()

我正在绘制一个 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"))

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

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

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

如果这对您的第一个代码块不起作用,请告诉我。