简体   繁体   English

如何在ggplot2的颜色图例中更改文本

[英]How to change the text in the colour legend of ggplot2

I have this code: 我有以下代码:

ggplot(databoth, aes(withxstep)) + 
       geom_point(aes(y = withnassoc, colour = "withnassoc"), size = 2.8) + 
       geom_point(aes(y = withoutnassoc, colour = "withoutnassoc"), size = 1 ) +
       labs(colour = "Legend") +
       labs(x = "Time") +
       labs(y = "N associations")

How do I modify the withnassoc and the withoutnassoc ? 如何修改withnassocwithoutnassoc I would like it to be "With Activities" and "Without activities". 我希望它是“有活动”和“无活动”。

This should answer your question: 这应该可以回答您的问题:

ggplot(databoth, aes(withxstep)) + 
       geom_point(aes(y = withnassoc, colour = "withnassoc"), size = 2.8) + 
       geom_point(aes(y = withoutnassoc, colour = "withoutnassoc"), size = 1 ) +
              labs(colour = "Legend", x = "Time", y = "N associations") +
              scale_color_manual(values = c("red", "blue"), 
                                 labels = c("With Activities", "Without activities"))

For this example data-set: 对于此示例数据集:

exampledata <- structure(list(withxstep = structure(c(4L, 3L, 2L, 1L), 
.Label = c("2017-06-27", "2017-06-28", "2017-06-29", "2017-06-30"), class = "factor"), 
withnassoc = c(1, 2, 3, 4), withoutnassoc = c(5, 6, 7, 8)), .Names = c("withxstep", 
"withnassoc", "withoutnassoc"), class = "data.frame", row.names = c(NA,-4L))

This would be the plot: 这将是情节:

在此处输入图片说明

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

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