0

I have a connected scatterplot using geom_point and geom_path. I want the shapes of the points to be different based on one grouping variable, which is the same variable I'm using to connect the points with geom_path. I manually chose shapes using scale_shape_manual(). I want to show only one legend with the title "Sample".

I've tried doing this using labs(shape = "Sample"), but this forces both legends to be visible and only names the first one, which I do not want to show. Without this line I get the correct single legend, but it is not named. How can I only show the legend I want but still rename it?

My code with labs():

ggplot(great_hollow, aes(x = d15N, y = Sample_ordered), color = Species_ordered) + 
  geom_path(aes(group = Species_ordered, colour = Species_ordered)) +
  geom_point(aes(shape = Species_ordered, fill = Species_ordered), colour = "black", size = 3) +
  scale_shape_manual(values = c(21, 22, 24, 21, 23, 25)) +
  labs(shape = "Sample") +
  facet_wrap(~Type, scales = "fixed") +
  ggtitle(expression(paste(delta^{15},"N Values by Site and Trophic Level"))) +
  scale_x_continuous(expression(paste(delta^{15}, "N (\u2030 VS air)"))) +
  scale_y_discrete(limits=rev) +
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y = element_blank(),
        plot.background = element_rect(color = 1, linewidth = 1), 
        plot.margin = margin(t = 10,  
                             r = 10,  
                             b = 10, 
                             l = 10))

The resulting plot: Two legends, wrong one is named

My code without labs():

ggplot(great_hollow, aes(x = d15N, y = Sample_ordered), color = Species_ordered) + 
  geom_path(aes(group = Species_ordered, colour = Species_ordered)) +
  geom_point(aes(shape = Species_ordered, fill = Species_ordered), colour = "black", size = 3) +
  scale_shape_manual(values = c(21, 22, 24, 21, 23, 25)) +
  facet_wrap(~Type, scales = "fixed") +
  ggtitle(expression(paste(delta^{15},"N Values by Site and Trophic Level"))) +
  scale_x_continuous(expression(paste(delta^{15}, "N (\u2030 VS air)"))) +
  scale_y_discrete(limits=rev) +
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y = element_blank(),
        plot.background = element_rect(color = 1, linewidth = 1), 
        plot.margin = margin(t = 10,  
                             r = 10,  
                             b = 10, 
                             l = 10))

The resulting plot: Single legend, unnamed

I'm relatively new to R so any suggestions would be much appreciated!

jguevara
  • 1
  • 2
  • 2
    Make the last line of your plot `labs(color = "Sample", shape = "Sample", fill = "Sample")`. Aesthetics mapped onto a single variable all need the same name if you want them merged in the legend. (Another option would be to rename `Sample_ordered` to `Sample` and just use your second code, with all instances of `Sample_ordered` changed to `Sample` – Allan Cameron Dec 14 '22 at 23:17
  • @AllanCameron perfect, the first option did exactly what I hoped for. Thank you! – jguevara Dec 14 '22 at 23:57

0 Answers0