ggplot()geom_smooth()颜色给我错误的颜色 [英] ggplot() geom_smooth() color gives me the wrong colors

查看:194
本文介绍了ggplot()geom_smooth()颜色给我错误的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定为什么我要向后退颜色。有人可以解释一下此代码中的颜色分配方式吗?我正在尝试在ggplot2参考网站上使用示例,但是我对此一直坚持很长时间。代码如下:

  #libraries 
库(ggplot2)
库(标度)
library(reshape2)

#data
client.data<-read.csv('client.total.sorted.csv',标头= TRUE,sep =,)

#plot
ggplot(数据= client.sorted)+
geom_smooth(映射= aes(x =日期,y =已取消,颜色=红色))+
geom_point(映射= aes(x =日期,y =已取消,颜色=红色)))+
geom_smooth(映射= aes(x =日期,y =有效,颜色=绿色)))+
geom_point(映射= aes(x =日期,y =有效,颜色=绿色))+
labs(title =激活与取消,x =日期,y =)

这是输出:

解决方案

我发现这篇文章引用了有助于解决此问题的传说:




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:

解决方案

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

这篇关于ggplot()geom_smooth()颜色给我错误的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆