如何设置geom_smooth平滑的数据

时间:2011-04-05 17:27:53

标签: r ggplot2 smoothing

这完全符合预期:

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

温度是根据数据框CETW的数据绘制的年份。该系列是平滑的。纯粹出于审美原因,我想为温暖的温度,正常的温度和寒冷的温度着色。有一个属性CETW $ classify,其值为“warm”,“normal”和“cold”。以下是预期颜色的数据:

p = qplot(year, temp, data=CETW, colour=classify, geom=c("point", "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")

2 个答案:

答案 0 :(得分:2)

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

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

在这里,您只在点图层中指定颜色美学,因此geom_smooth不考虑这一点,并且只为您提供一条线。

让我知道这是否有效

答案 1 :(得分:0)

完成@Ramnath建议的另一种方法是使用group的{​​{1}}参数。

aes()