简体   繁体   English

ggplot图例中缺少标签

[英]ggplot missing labels in legend

I want to plot some data and then add a regression line and horizontal lines for the mean and mean +/- the standard deviation, each in a different colour. 我想绘制一些数据,然后为平均值和平均值+/-标准偏差添加回归线和水平线,每种颜色均不同。 I have managed to do this but I can't get the legend right. 我已经做到了,但我无法正确理解图例。 It shows labels for the first two but says nothing about the three horizontal lines. 它显示了前两个的标签,但对三个水平线没有任何说明。 How can I get the legend to have a label for each of these three lines too? 我怎样才能使图例也为这三行都贴上标签?

Another post mentioned show_guide = TRUE, but this didn't work for me. 另一篇文章提到show_guide = TRUE,但这对我不起作用。 I've also been looking at the documentation for scale_colour_manual but that didn't help me. 我也一直在寻找scale_colour_manual的文档,但这对我没有帮助。

My code is this: 我的代码是这样的:

p <- qplot(mpg, wt, data = mtcars, colour="1")
p <- p + geom_smooth(method='lm',aes(x=mpg,y=wt,colour="2"),formula=y~x)
p <- p + geom_hline(yintercept = mean(mtcars$wt), colour = "3")
p <- p + geom_hline(yintercept = mean(mtcars$wt) + sd(mtcars$wt), colour = "3",    linetype="dashed")
p <- p + geom_hline(yintercept = mean(mtcars$wt) - sd(mtcars$wt), colour = "3", linetype="dashed")      
p <- p + labs(colour="")    
p <- p + scale_colour_manual(values = c("red","blue", "green","green","green"),labels=c("Data","Regression","Mean","Mean + SD","Mean - SD"))
p <- p + guides(colour = guide_legend())
print(p)

Any help will be much appreciated, cheers! 任何帮助将不胜感激,加油!

I think this is probably closer to what you want: 我认为这可能更接近您想要的:

d <- data.frame(yint = c(mean(mtcars$wt) - sd(mtcars$wt),
                mean(mtcars$wt),
                mean(mtcars$wt) + sd(mtcars$wt)),
                grp = c('dashed','solid','dashed'))

p <- ggplot(data = mtcars,aes(mpg, wt)) + 
    geom_point(aes(color = "Data")) +
    geom_smooth(method='lm',aes(x=mpg,y=wt,color = "Regression"),formula=y~x) + 
    geom_hline(data = d,aes(yintercept = yint,linetype = grp,color = "Mean +/- SD")) + 
    scale_linetype_manual(values = c('dashed' = 'dashed','solid' = 'solid')) + 
    labs(color = "")
print(p)

As to the question about labeling each of the horizontal lines separately, my answer is that that doesn't belong in the legend at all. 关于分别标记每条水平线的问题,我的回答是,这根本不属于图例。 Those should be labeled with geom_text() : 这些应该用geom_text()标记:

d <- data.frame(yint = c(mean(mtcars$wt) - sd(mtcars$wt),
                         mean(mtcars$wt),
                         mean(mtcars$wt) + sd(mtcars$wt)),
                         grp = c('dashed','solid','dashed'),
                         lab = c('-SD','Mean','+SD'))

p <- ggplot(data = mtcars,aes(mpg, wt)) + 
    geom_point(aes(color = "Data")) +
    geom_smooth(method='lm',aes(x=mpg,y=wt,color = "Regression"),formula=y~x) + 
    geom_hline(data = d,aes(yintercept = yint,linetype = grp),color = "green") + 
    geom_text(data = d,aes(y = yint,label = lab),
              vjust = -0.5,hjust = 1,x = Inf,size = 3) +
    scale_linetype_manual(values = c('dashed' = 'dashed','solid' = 'solid')) + 
    labs(color = "")
print(p)

Joran's answer inspired me to make some changes and now I have managed to write some code that gives me the plot and legend I was looking for: Joran的答案启发了我进行一些更改,现在我设法编写了一些代码,这些代码为我提供了所寻找的情节和传说:

n <- ncol(mtcars)
mtcars[,n+1] <- mean(mtcars$wt)
mtcars[,n+2] <- mean(mtcars$wt) + sd(mtcars$wt)
mtcars[,n+3] <- mean(mtcars$wt) - sd(mtcars$wt)
p <- qplot(mpg,wt,data = mtcars, colour="1")
p <- p + geom_smooth(method='lm',aes(x=mpg,y=wt,colour="2"),formula=y~x)
p <- p + geom_line(aes(x=mpg,y=mtcars[,n+1],colour="3"))
p <- p + geom_line(aes(x=mpg,y=mtcars[,n+2],colour="4"),linetype="dashed")
p <- p + geom_line(aes(x=mpg,y=mtcars[,n+3],colour="5"),linetype="dashed")
p <- p + labs(colour="")    
p <- p + scale_colour_manual(values = c("red","blue",  "green","green","green"),labels=c("Data","Regression","Mean","Mean + SD","Mean - SD"))
p <- p + guides(colour = guide_legend())
print(p)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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