gpt4 book ai didi

r - ggplot2 多个 stat_smooth : change color & linetype

转载 作者:行者123 更新时间:2023-12-03 14:12:08 26 4
gpt4 key购买 nike

我无法使用多个平滑器 (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
...

有八个具有相应时间戳(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")

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

我想要的是:

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

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

最佳答案

好吧,您的数据对于重新创建绘图并不是那么有用,所以我创建了一个不同的示例数据集

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

而且我不确定您到底尝试了什么,但是 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"))

enter image description here

关于r - ggplot2 多个 stat_smooth : change color & linetype,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24828498/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com