0

这完全符合预期:

p = qplot(year, temp, data=CETW, geom=c("point", "smooth"))

从数据框 CETW 中获取数据,绘制温度与年份的关系图。该系列被平滑。纯粹出于审美原因,我想给温暖的温度、正常的温度和寒冷的温度上色。有一个属性 CETW$classify,其值为“暖”、“正常”和“冷”。以下以预期颜色显示数据:

p = qplot(year, temp, data=CETW, colour=classify, geom=c("point", "smooth"))

但是现在“平滑”的东西已经决定变得聪明,并对三个温度中的每一个都应用了单独的平滑曲线。这很愚蠢,因为数据点太少。那么我如何告诉smooth 来平滑整个系列的温度,就像它在第一种情况下所做的那样?很高兴看到一个使用 stat_smooth 和 method=loess 的答案。

根据要求,我正在使用 dput 添加 CETW 的数据:

structure(list(year = 1959:2011, temp = c(4.5, 5.08, 5.73, 3.43, 
1.25, 3.7, 3.8, 4.95, 5.6, 4.2, 3.2, 3.4, 4.55, 5.33, 5.2, 5.5, 
6.03, 5.13, 4.23, 4.75, 2.35, 4.63, 5.35, 3.45, 4.8, 4.35, 3.2, 
3.4, 3.68, 5.55, 6.75, 6.75, 4.25, 5.33, 5.2, 5.43, 5.83, 3.4, 
5.13, 6.55, 5.93, 5.95, 4.65, 5.93, 5.4, 5.48, 5.73, 4.33, 6.63, 
5.75, 4.4, 3.35, 4.03), classify = c("normal", "normal", "normal", 
"normal", "cold", "normal", "normal", "normal", "normal", "normal", 
"cold", "cold", "normal", "normal", "normal", "normal", "warm", 
"normal", "normal", "normal", "cold", "normal", "normal", "normal", 
"normal", "normal", "cold", "cold", "normal", "normal", "warm", 
"warm", "normal", "normal", "normal", "normal", "normal", "cold", 
"normal", "warm", "normal", "normal", "normal", "normal", "normal", 
"normal", "normal", "normal", "warm", "normal", "normal", "cold", 
"normal")), .Names = c("year", "temp", "classify"), row.names = c(NA, 
-53L), class = "data.frame")
4

2 回答 2

2

您应该使用 ggplot,并在本地指定选项。这是它的工作原理

p = ggplot(data = CETW, aes(x = year, y = temp)) +
    geom_point(aes(colour = classify)) +
    geom_smooth()

在这里,您仅在点层中指定颜色美学,因此 geom_smooth 不考虑这一点,只给您一条线。

让我知道这个是否奏效

于 2011-04-05T17:35:21.833 回答
0

完成@Ramnath 建议的另一种方法是使用group参数 of aes()

ggplot(data=CETW, mapping=aes(x=year, y=temp, colour=classify)) +
+ geom_point() + geom_smooth(aes(group=1))
于 2013-12-13T14:32:05.847 回答