ggplot2 multiple stat_smooth:更改颜色和线型

phi*_*lka 6 r ggplot2

我无法使用多个更平滑的方式更改当前绘图的颜色和线型(stat_smooth())

这里是数据结构的概述:

     serviceInstanceName            timestamp     value
1    DE1Service-utilityPredicted    2014-02-22    10.000000
2    SE1Service-utilityPredicted    2014-02-22     4.385694
3    DE2Service-utilityPredicted    2014-02-22     0.000000
4    US1Service-utilityPredicted    2014-02-22     2.230000
5    DE1Service-utilityActual       2014-02-22    10.000000
6    SE1Service-utilityActual       2014-02-22     8.011919
7    DE2Service-utilityActual       2014-02-22     3.000000
8    US1Service-utilityActual       2014-02-22     1.325191
...
Run Code Online (Sandbox Code Playgroud)

根据时间戳(y轴)和值(x轴),有八个唯一的服务实例.

这里的代码:

ggplot(rmm, aes(x=timestamp, y=value, color=serviceInstanceName, group=serviceInstanceName)) 
+ stat_smooth(size=1.5, method = "loess", level = 0.95, fullrange = TRUE, se = FALSE)
+ scale_x_datetime(breaks = date_breaks("1 day"), labels = date_format("%a/%m"))
+ theme(axis.text.x = element_text(angle = 90, hjust = 1)) + xlab("Day") 
+ ylab("Utility") + ggtitle("Utility Trend")
Run Code Online (Sandbox Code Playgroud)

这里的情节: http://imgur.com/Tk02YXC

我想要的是:

=>手动更改每个唯一*serviceInstanceName'属性值的线型和颜色.我尝试了很多东西来自scale_color_manual() ..直到提取更平滑的值...但实际上无法解决这个问题.

任何帮助表示赞赏.谢谢!

MrF*_*ick 5

好吧,你的数据不是那么重新创建情节,所以我创建了一个不同的样本数据集

rmm<-data.frame(
    timestamp = as.POSIXct(rep(seq(as.Date("2014-01-01"), 
        as.Date("2014-01-10"), by="1 day"),5)),
    serviceInstanceName = rep(letters[1:5], each=10),
    value = cumsum(rnorm(50))
)
Run Code Online (Sandbox Code Playgroud)

我不确定你尝试了什么,但scale_color_manual应该有效.如果你想改变你需要在中设置的线型aes()

library(ggplot2)
library(scales)

ggplot(rmm, aes(x=timestamp, y=value, 
    color=serviceInstanceName, linetype=serviceInstanceName)) +
stat_smooth(size=1.5, method = "loess", level = 0.95, 
    fullrange = TRUE, se = FALSE) + 
scale_x_datetime(breaks = date_breaks("1 day"), 
    labels = date_format("%a/%m")) + 
theme(axis.text.x = element_text(angle = 90, hjust = 1)) + xlab("Day")  + 
ylab("Utility") + ggtitle("Utility Trend") + 
scale_color_manual(values=c(a="orange",b="yellow",
    c="red", d="sienna",e="cornsilk"))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述