0

I'm trying to make a scatter plot with overlapping subsets of data. I'm successful to produce the plot but I'm struggling with placing the legend. In this dataset, I want to give different colors and shapes for points matching with covid-19 and the Russia-Ukraine War.

df %>%
ggplot(aes(x=CovidAid, y=FoodInflation)) +
geom_point() +
geom_point(data = df %>% filter(Covid=="COVID"), pch=24, size=3, color="red") +
geom_point(data = df %>% filter(War=="War"), pch=25, size=3, color="red") +
labs(y="Food Inflation", x="COVID-19 Assistance, million USD") +
scale_y_continuous(labels = scales::percent, breaks = seq(-0.01,0.12,0.02)) +
theme_bw() +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
axis.text = element_text(color = "black"), axis.title = element_text(face = "bold"),
legend.position = "bottom", legend.justification = "center", legend.direction = "horizontal")

I tried scale_color_manual() and scale_shape_manual() by putting values for those three categories but didn't work.

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Welcome to SO! If you want a legend you have to map on aesthetics, i.e. do e.g. `geom_point(data = df %>% filter(Covid=="COVID"), aes(shape = "Covid", color = "Covid"), size=3)`. Afterwards you could set your desired colors and shape using `scale_xxx_manual`. For more help you please provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. – stefan Apr 27 '23 at 05:36

0 Answers0