我不知道为什么我的颜色会落后。有人可以解释一下这段代码中的颜色是如何分配的?我正在尝试在ggplot2参考站点上使用示例,但我已经坚持了很长时间。这是代码:
#libraries
library(ggplot2)
library(scales)
library(reshape2)
#data
client.data <- read.csv('client.total.sorted.csv', header = TRUE, sep = ",")
#plot
ggplot(data = client.sorted) +
geom_smooth(mapping = aes(x = Date, y = Cancelled, color = "red")) +
geom_point(mapping = aes(x = Date, y = Cancelled, color = "red")) +
geom_smooth(mapping = aes(x = Date, y = Active, color = "green")) +
geom_point(mapping = aes(x = Date, y = Active, color = "green")) +
labs(title = "activations vs cancellations", x = "Date", y = "")
答案 0 :(得分:1)
我发现这篇文章引用的传说帮助我解决了这个问题:
Add legend to ggplot2 line plot
对我有用的解决方案是:
ggplot(data = client.sorted) +
geom_smooth(mapping = aes(x = Date, y = Cancelled, color = "CancelledLines")) +
geom_point(mapping = aes(x = Date, y = Cancelled, color = "CancelledPoints")) +
geom_smooth(mapping = aes(x = Date, y = Active, color = "greenLines")) +
geom_point(mapping = aes(x = Date, y = Active, color = "greenPoints")) +
scale_color_manual("",
breaks = c("CancelledLines", "CancelledPoints", "greenLines", "greenPoints"),
values = c("red", "red", "green", "green")) +
labs(title = "activations vs cancellations", x = "Date", y = "")