简体   繁体   English

ggplot图例为什么显示“ colour”参数?

[英]Why does the ggplot legend show the “colour” parameter?

I have the following example: 我有以下示例:

data <- structure(list(a = c(-1.25549186262767, -0.840855480786298, -
0.635371312524283, 
-0.602907981454667, -0.472166385166945, -0.285773634866154, 0.0701427664273268, 
0.138108224803923, 1.38435934347858, 1.71144087270237), b = c(-3.44400412039417, 
0.675644682353751, -1.04793816522475, -7.38303347186651, 2.34519166466874, 
0.334780748573386, 4.76806919070976, 4.8633533150074, 3.50106026731172, 
-1.27172351054143), c = c(-3.02376206439776, -2.56390769080574, 
-1.48659913867609, -1.27976202274701, -0.368725655874139, 1.08537150160227, 
3.98619381956471, 4.50687017428731, 4.10341582203292, -1.61769414438858
), d = c(5.71851494232005, 2.90539833491649, 2.75195159216204, 
2.73478241733301, 2.65941820902101, 2.60630235726839, 3.34836154776286, 
3.62938300664006, 4.61153521538016, 5.56230567213863), e = c(8.98703236551896, 
4.5660296657415, 4.32487774825464, 4.29789523068949, 4.17945528847841, 
4.09598014088541, 5.26217626511884, 5.70382046327322, 7.24733897758039, 
8.74153894964533)), .Names = c("a", "b", "c", "d", "e"), row.names = c(NA, 
-10L), class = "data.frame")

ggplot(data, aes(x=a, y=b)) + geom_point() + 
    geom_line(aes(x=a, y=c)) + 
    geom_line(aes(x=a, y=(c - d), colour="red")) +
    geom_line(aes(x=a, y=(c + d), colour="red")) +
    geom_line(aes(x=a, y=(c - e), colour="blue")) +
    geom_line(aes(x=a, y=(c + e), colour="blue")) 

I want the labels to be "d" and "e", but they are instead being given the value of the "colour" field. 我希望标签为“ d”和“ e”,但是它们被赋予“颜色”字段的值。 Two questions: 两个问题:

1) How can I eliminate the legend entirely? 1)如何完全消除图例? 2) If I want to include the legend, how can I have it set to specific values rather than the colour? 2)如果要包括图例,如何将其设置为特定值而不是颜色?

The important thing to remember here is that items inside of aes() will map data to aesthetics. 这里要记住的重要一点是, aes()内的项目会将数据映射到美学。 If you are setting a constant value(s), then you can move that information outside of aes() . 如果要设置一个恒定值,则可以将该信息移到aes() A slight modification of your code yields: 稍微修改一下代码即可得出:

ggplot(data, aes(x=a, y=b)) + geom_point() + 
geom_line(aes(x=a, y=c)) + 
geom_line(aes(x=a, y=(c - d)), colour="red") +
geom_line(aes(x=a, y=(c + d)), colour="red") +
geom_line(aes(x=a, y=(c - e)), colour="blue") +
geom_line(aes(x=a, y=(c + e)), colour="blue") 

Which gives you what you are after without any legend. 这给了你所追求的,没有任何传说。 I like @koshke's approach for including a legend above so won't duplicate that. 我喜欢@koshke在上面添加图例的方法,因此不会重复。 The other approach you could take would be to perform your data manipulation outside of the call to ggplot2() and then melt() it into long format before plotting. 您可以采用的另一种方法是在对ggplot2()的调用之外执行数据操作,然后在绘制之前将其melt()为长格式。 That would shorten your call to ggplot() since you could get rid of the multiple calls to geom_line() , but there's obviously the overhead of preprocessing the data. 这可以缩短对ggplot()调用,因为您可以摆脱对geom_line()的多次调用,但是显然存在预处理数据的开销。 Probably 6 in one, 1/2 dozen in the other for this problem, but something to keep in mind for future problems. 这个问题大概是六分之一,另一个是1/2打,但是对于将来的问题要牢记。

is this what you want to do? 这就是你想做的吗?

ggplot(data, aes(x=a, y=b)) + geom_point() + 
    geom_line(aes(x=a, y=c)) + 
    geom_line(aes(x=a, y=(c - d), colour="d")) +
    geom_line(aes(x=a, y=(c + d), colour="d")) +
    geom_line(aes(x=a, y=(c - e), colour="e")) +
    geom_line(aes(x=a, y=(c + e), colour="e")) +
    scale_colour_manual(name="legend title", 
                        values=c("red", "blue"),
                        breaks=c("d", "e"))

也可以通过添加+ opts(legend.position = "none")选项来消除标签。

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

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