ggplot() geom_smooth() color gives me the wrong co

2020-01-19 01:23发布

问题:

I'm not sure why I'm getting my colors backward. Can someone please explain how the colors are assigned in this code? I'm trying to use examples on ggplot2 reference site, but I've been stuck on this for a long time. Here's the code:

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

Here's the output:

回答1:

I found this post referencing legends that helped me solve this:

Add legend to ggplot2 line plot

The solution that worked for me is this:

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