【问题标题】:How to set the data that geom_smooth smoothes如何设置geom_smooth平滑的数据
【发布时间】:2011-04-05 17:27:53
【问题描述】:

这完全符合预期:

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

【问题讨论】:

  • 您能提供一些示例数据吗?尝试使用dput(CETW)
  • 这不是答案!提出上述问题的帐户似乎已经消失了。 Ramnath 给出的答案很好 - 非常感谢。

标签: r ggplot2 smoothing


【解决方案1】:

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

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

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

让我知道这是否有效

【讨论】:

    【解决方案2】:

    实现@Ramnath 建议的另一种方法是使用aes()group 参数。

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

    【讨论】:

      猜你喜欢
      • 2023-03-05
      • 1970-01-01
      • 2021-07-16
      • 1970-01-01
      • 2023-02-25
      • 2012-10-28
      • 1970-01-01
      • 2015-05-16
      • 1970-01-01
      相关资源
      最近更新 更多