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

时间:2017-07-28 17:34:19

标签: r ggplot2 legend

我有这段代码:

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")

如何修改withnassocwithoutnassoc?我希望它是“有活动”和“没有活动”。

1 个答案:

答案 0 :(得分:2)

这应该回答你的问题:

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"))

对于此示例数据集:

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))

这将是情节:

enter image description here