简体   繁体   中英

ggplot legend: geom_abline interference

Objective: to have a color legend for geom_point showing a colored dot for each group together with a legend for geom_abline showing one colored line for each line.

Am I doing something wrong? Is there a solution?

# data: mtcars + some made-up straight lines
library(ggplot2)
df = data.frame(Intercept = c(2,3,4), Slope = c(0,0,0), Group = factor(c(1, 2, 3)))

Comment about #1 : There's nothing special about the basic plot, but I have grouped the data inside the aes() and made color an aes() . I think it is standard to have both "group" and "color" inside the aes to achieve grouping and coloring.

# 1. my basic plot
ggplot(data = mtcars, aes(x = mpg, y = wt, group = vs, color = factor(vs))) + 
    geom_point() -> p

Comment about #2 : Clearly I did not set up ggplot right to handle the legend properly. I also tried to add group = Group inside the aes. But there is a somewhat more serious problem: geom_point forms 2 groups, geom_abline forms 3 groups, but the legend is showing only 4 color/line combinations. One of them has been merged (the green one). What have I done wrong here?

# 2. my naive attempt to add ablines of 3 different colours
p + geom_abline(data = df, aes(intercept = Intercept, slope = Slope,
                           colour = Group))

在此处输入图片说明

Comment about #3 : The ablines have been removed in the legend, but the points are still not right. From here on it gets more and more desperate.

# 3. Suppress the ab_line legend?
p + geom_abline(data = df, aes(intercept = Intercept, slope = Slope,
                           colour = Group), show.legend = FALSE)

在此处输入图片说明

Comment about #4 : This is what I'm going for at the moment. Better no legend than a wrong legend. Shame about losing the colors though.

# 4. Remove the geom_abline legend AND colors
p + geom_abline(data = df, aes(intercept = Intercept, slope = Slope))

在此处输入图片说明

Comment #5 : I don't know what I was hoping here... that if I defined the data and aes inside the call to geom_point() rather than the ggplot() , somehow geom_abline()) would not hijack the colors and legend, but no, it does not appear to make a difference.

# 5. An attempt to set the aes inside geom_point() instead of ggplot()
ggplot() + 
    geom_point(data = mtcars, aes(x = mpg, y = wt, group = vs, color = factor(vs))) + 
    geom_abline(data = df, aes(intercept = Intercept, slope = Slope, color = "groups")) +
    scale_color_manual(values = c("red", "blue", "black"))

在此处输入图片说明

One option would be to use a filled shape for the mtcars data, then you can have a fill scale and a colour scale, rather than two colour scales. You could add an option such as colour="white" to the geom_point statement in order to change the colour of the edges of the points, if you don't want the black outlines.

library(ggplot2)
df = data.frame(Intercept = c(2,3,4), Slope = c(0,0,0), Group = factor(c(1, 2, 3)))
ggplot(data = mtcars, aes(x = mpg, y = wt, group = vs, fill = factor(vs))) + 
           geom_point(shape=21, size=2) +
           geom_abline(data = df, aes(intercept = Intercept, slope = Slope,
                              colour = Group))

在此处输入图片说明

if you need or want a horizontal line in the legend you might consider using this code:

library(ggplot2)
df = data.frame(Intercept = c(2,3,4), Slope = c(0,0,0),
Group = factor(c(1, 2, 3)))
ggplot(data = mtcars,
   aes(x = mpg, y = wt, group = vs, fill = factor(vs))) + 
geom_point(shape=21, size=2) +
geom_hline(data = df,
          aes(yintercept = Intercept,colour = Group))

plot with geom_hline

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM