One way to get a legend is to add a points layer based on the dataset in long format, mapping color
to the grouping variable.
First, make a long format dataset via gather
from tidyr.
df2 = tidyr::gather(df, group, value, -trt)
Then make the plot, adding the new points layer with the long dataset and using scale_color_manual
to set colors. I moved the geom_dumbbell
specific aesthetics into that layer.
ggplot(df, aes(y = trt)) +
geom_point(data = df2, aes(x = value, color = group), size = 3) +
geom_dumbbell(aes(x = l, xend = r), size=3, color="#e3e2e1",
colour_x = "red", colour_xend = "blue",
dot_guide=TRUE, dot_guide_size=0.25) +
theme_bw() +
scale_color_manual(name = "", values = c("red", "blue") )
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…