1
votes

I've created a plot using geom_segment and I'd like to add a legend for the different line colours. The only solution I've found suggests modifying the linetype in aes(), but this only works for one segment - when I add a second label to another geom_segment then the linetype becomes dashed and the legend just changes to the new colour.

Here's the code for the plot:

library(ggplot2)

rib_x <- seq(1,10,0.5)
rib_ymin <- seq(3,12,0.5)
rib_ymax <- c(3.0,4.4,5.55,6.55,7.28,8.1,8.6,9.1,9.52,9.98,10.3,10.62,10.98,11.2,11.4,11.52,11.7,11.8,12)

ggplot(data.frame())+
  geom_segment(aes(x=1, xend=10, y=12, yend=3),colour="dark red",size=1.5)+
  geom_segment(aes(x=1, y=3,xend=10,yend=12),colour="green",size=1.5)+
  stat_smooth(aes(x=rib_x,y=rib_ymax),se=FALSE,colour="dark green",size=1.5)+
  xlab("Agroecological zone")+
  ylab("Productivity")+
  geom_segment(aes(x=0, xend = 0, y=2, yend=12), size=1.5, arrow=arrow(length=unit(0.6,"cm")))+
  theme_bw()+
  annotate("text", label="Arid", x=2, y=1)+
  annotate("text", label="Semi-arid", x=5, y=1)+
  annotate("text", label="Humid", x=8, y=1)+
  theme(axis.title=element_text(size=14),axis.text=element_blank(),legend.title=element_blank(),
        axis.ticks=element_blank(),panel.border=element_blank(),
        panel.grid.major = element_blank(),panel.grid.minor = element_blank())

I'd like to add a legend for the three line colours, where I can also specify the text used in the legend.

Many thanks!

1

1 Answers

1
votes

Move the colour aesthetics inside aes() and name them with the actual strings you want to use to label each color. Then add scale_colour_manual() at the end to get the specific colors you want. See the example below.

In the "standard" ggplot2 workflow, you would map some categorical column to colour inside aes() (e.g., ggplot(iris, aes(x=Petal.Width, y=Sepal.Width, colour=Species)) + geom_smooth(se=FALSE, method="lm")) and then the geoms (e.g., points, lines, smooths, etc.) would appear in a different color for each unique value of the categorical column that was mapped to colour. Here, we're not using a data frame, so we create "dummy" mappings to the colour aesthetic. Putting them inside aes() causes ggplot to generate the legend.

ggplot() +
  geom_segment(aes(x=1, xend=10, y=12, yend=3, colour="name1"), size=1.5) +
  geom_segment(aes(x=1, y=3,xend=10,yend=12, colour="name2"), size=1.5) +
  stat_smooth(aes(x=rib_x,y=rib_ymax, colour="name3"),se=FALSE,size=1.5) +
  labs(x="Agroecological zone", y="Productivity", colour="Type") +
  geom_segment(aes(x=0, xend = 0, y=2, yend=12), size=1.5, arrow=arrow(length=unit(0.6,"cm"))) +
  annotate("text", label="Arid", x=2, y=1) +
  annotate("text", label="Semi-arid", x=5, y=1) +
  annotate("text", label="Humid", x=8, y=1) +
  theme_void() +
  theme(axis.title=element_text(size=14, margin=margin(b=3)),
        axis.title.y=element_text(angle=90)) +
  scale_color_manual(values=c("name1"="darkred", "name2"="green", "name3"="darkgreen"))

enter image description here