ggplot2 multiple stat_smooth:改变颜色&线型 [英] ggplot2 multiple stat_smooth: change color & linetype

查看:452
本文介绍了ggplot2 multiple stat_smooth:改变颜色&线型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

的数据结构:

  serviceInstanceName时间戳值
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
.. 。

根据时间戳(y轴)和值(x轴),有八个独特的服务实例。



这里的代码:

  ggplot(rmm,aes(x =恬estamp,y = value,color = serviceInstanceName,group = serviceInstanceName))
+ stat_smooth(size = 1.5,method =loess,level = 0.95,fullrange = TRUE,se = FALSE)
+ scale_x_datetime (break = 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)

这里的情节:



我想要的:

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



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

解决方案

好吧,您的数据并不适合重新创建剧情,所以我创建了一个不同的示例数据集 p>

  rmm <-data.frame(
timestamp = as.POSIXct(rep(seq(as.Date(2014- ),
as.Date(2014-01-10),by =1 day),5)),
serviceInstanceName = rep(letters [1:5],每个= 10),
值= cumsum(rnorm(50))

我不确定你试过的是什么,但是 scale_color_manual 应该可以工作。如果您想更改线型,您需要在 aes()中设置该线型
$ b

  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(效用趋势)+
scale_color_manual(值= c(a =orange,b =yellow,
c =red ,d =sienna,e =cornsilk))


I am not able to change the colors and linetypes of my current plot with multiple smoother ( stat_smooth())

Here an overview of the data structure:

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

There are eight unique service instances with according timestamp (y axis) and value (x axis).

Here the code:

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

Here the plot:

What I want:

=> To manually change the linetypes and colors of each unique *serviceInstanceName' attribute value. I tried many things from scale_color_manual() .. until extracting the value of the smoother.. but really could not solve this.

Any help is appreciated. Thanks!

解决方案

Well, your data wasn't that hlpeful for recreating the plot so i created a different sample data set

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

And i'm not sure exactly what you tried, but scale_color_manual should have worked. And if you want to change the line type you need to set that in the 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"))

这篇关于ggplot2 multiple stat_smooth:改变颜色&amp;线型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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