AntVal
AntVal

Reputation: 665

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

I am plotting a sf object (multipolygons). I which its geometries are filled with a colour according to each observations' value in the column cand_dummy. However, I have tried to change many things in this plot but either I get the geometries with colour, but I can't change the colour, with something like this:

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

This prints different colours for different values of cand_dummy, but not the colours I am setting it for on scale_fill_manual. Alternatively, I changed fill for colour in the aes and do the following:

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

But now I get the boundaries/borders of each geometry in these colours while the geometry itself is coloured as grey. So I can't find a way of getting what I am looking for. Any idea? Thanks!

Upvotes: 3

Views: 4415

Answers (1)

Joel Kandiah
Joel Kandiah

Reputation: 1525

I believe that the issue is that you have included both functions scale_fill_manual and scale_fill_discrete in the code. I believe you should have only the following function:

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

Let me know if this doesn't work for your first code block.

Upvotes: 3

Related Questions