Ggplot2 multiple stat_smooth: change color and line type

I can’t change the colors and linetypes of my current chart with smoother (stat_smooth ())

Here is 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 instances of the service with the corresponding timestamp (y axis) and value (x axis).

Here is 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's the plot: http://imgur.com/Tk02YXC

What I want:

=> To manually change the line types and colors of each unique value of the * serviceInstanceName attribute. I tried a lot of things from scale_color_manual () .. before retrieving the smoothing value .. but really couldn't solve it.

Any help is appreciated. Thanks!

+6
source share
1 answer

Well, your data was not so useful for reconstructing the graph, so I created another data set with samples

 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 don’t know exactly what you tried, but scale_color_manual should have worked. And if you want to change the line type, you need to set it to 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")) 

enter image description here

+5
source

Source: https://habr.com/ru/post/972460/


All Articles