0

I have create a plot with two aesthetics color and shape, but only legend of color is displayed not the shape.

sav %>% 
  ggplot(aes(x = Subject, y = Percentage, color = Subject, shape = Location)) +
  geom_point(data = filter(sav, Location == "IRELAND"), shape = "square",  size = 3) + 
  geom_point(data = filter(sav, Location == "OECD"), shape = "diamond",  size = 3) +
  geom_point(data = filter(sav, !Location %in% c("OECD", "IRELAND")), shape = "circle", 
             size = 2, position = "jitter") +
  coord_flip() +
  theme(legend.position = "top")+
  theme_light()

dput(sav):

structure(list(Location = c("CANADA", "CANADA", "FRANCE", "FRANCE", 
"GERMANY", "GERMANY", "IRELAND", "IRELAND", "ITALY", "ITALY", 
"NETHERLANDS", "NETHERLANDS", "SPAIN", "SPAIN", "UNITED STATES", 
"UNITED STATES", "OECD", "OECD", "CANADA", "ITALY", "SPAIN", 
"FRANCE", "IRELAND", "UNITED STATES", "NETHERLANDS", "OECD", 
"GERMANY"), Subject = c("Food", "Total", "Food", "Total", "Food", 
"Total", "Food", "Total", "Food", "Total", "Food", "Total", "Food", 
"Total", "Food", "Total", "Food", "Total", "Total_Minus_Food_Energy", 
"Total_Minus_Food_Energy", "Total_Minus_Food_Energy", "Total_Minus_Food_Energy", 
"Total_Minus_Food_Energy", "Total_Minus_Food_Energy", "Total_Minus_Food_Energy", 
"Total_Minus_Food_Energy", "Total_Minus_Food_Energy"), Frequency = c("Monthly", 
"Monthly", "Monthly", "Monthly", "Monthly", "Monthly", "Monthly", 
"Monthly", "Monthly", "Monthly", "Monthly", "Monthly", "Monthly", 
"Monthly", "Monthly", "Monthly", "Monthly", "Monthly", "Monthly", 
"Monthly", "Monthly", "Monthly", "Monthly", "Monthly", "Monthly", 
"Monthly", "Monthly"), Time = c("2022-12", "2022-12", "2022-12", 
"2022-12", "2022-12", "2022-12", "2022-12", "2022-12", "2022-12", 
"2022-12", "2022-12", "2022-12", "2022-12", "2022-12", "2022-12", 
"2022-12", "2022-12", "2022-12", "2022-12", "2022-12", "2022-12", 
"2022-12", "2022-12", "2022-12", "2022-12", "2022-12", "2022-12"
), Percentage = c(11.02015, 6.319445, 12.86678, 5.850718, 19.75631, 
8.550855, 11.74636, 8.224299, 13.14815, 11.63227, 16.7983, 9.586879, 
15.68565, 5.70769, 11.88275, 6.454401, 15.60381, 9.438622, 5.58275, 
4.469475, 4.442303, 3.36004, 4.999758, 5.707835, 6.034873, 7.221961, 
5.05511)), class = "data.frame", row.names = c(NA, -27L))

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • I tried your code with the [`storms`](https://dplyr.tidyverse.org/reference/storms.html) dataset but I was unable to recreate your issue. With the `storms` dataset, your code created two legends, a color legend and a shape legend. Can you make your post [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and provide your data using `dput(sav)`? You may also benefit from looking at this question: https://stackoverflow.com/questions/26218002/r-manually-set-shape-by-factor – jrcalabrese Feb 12 '23 at 16:11
  • 1
    Hi, I have added dput(sav) to the pos, thank you – Smitesh Patil Feb 12 '23 at 16:17

2 Answers2

1

From looking at your logic, you really only need a single geom_point layer. Perhaps it would be best to lump all non-Ireland, non-OECD countries together to do this:

sav %>% 
  mutate(Location = ifelse(Location %in% c("OECD", "IRELAND"), 
                           Location, "OTHER")) %>%
  ggplot(aes(x = Subject, y = Percentage, color = Subject, shape = Location)) +
  geom_point(size = 3, position = "jitter") +
  coord_flip() +
  theme(legend.position = "top")+
  theme_light()

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
1

First, if you want your legend on top, make sure to specify that after theme_light().

Additionally, if you want to avoid creating a separate variable for "countries that aren't Ireland or OCED", you can remove it from the legend entirely by specifying "circle" for those countries and then Ireland and OCED get specified using scale_shape_manual(). You just have to specify aes(shape = Location) for your first two geom_point() lines that reference "IRELAND" and "OCED".

library(tidyverse)
sav %>% 
  ggplot(aes(x = Subject, y = Percentage, color = Subject)) + 
  geom_point(data = filter(sav, Location == "IRELAND"), aes(shape = Location), size = 3) + 
  geom_point(data = filter(sav, Location == "OECD"), aes(shape = Location), size = 3) +
  geom_point(data = filter(sav, !Location %in% c("OECD", "IRELAND")), shape = "circle", size = 2, position = "jitter") +
  coord_flip() + theme_light() +
  theme(legend.position = "top") +
  scale_shape_manual(values = c("square", "diamond"))

enter image description here

jrcalabrese
  • 2,184
  • 3
  • 10
  • 30