如何设置geom_smooth平滑的数据

发布于 2024-10-30 17:21:32 字数 1487 浏览 4 评论 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")

This works exactly as expected:

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

The temperature is plotted against the year taking the data from the data frame CETW. The series is smoothed. Purely for aesthetic reasons I wanted to colour the warm temperatures, the normal temperatures, and the cold temperatures. There is an attribute CETW$classify whose values are "warm", "normal" and "cold". The following presents the data in the expected colours:

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

but now the "smooth" thingy has decided to be clever and has applied separate smoothing curves to each of the three temperatures. This is silly because there are too few data points. So how do I tell smooth to smooth the whole series of temperatires as it did in the first case? It would be nice to see an answer that uses stat_smooth with method=loess.

As requested I am adding the data for CETW using dput:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弄潮 2024-11-06 17:21:32

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

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

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

让我知道这是否有效

You should use ggplot, and specify options locally. Here is how it would work

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

Here you specify the colour aesthetic only in the point layer and hence geom_smooth does not take that into account and gives you only a single line.

Let me know if this works

十秒萌定你 2024-11-06 17:21:32

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

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

Another way of accomplishing the suggestion of @Ramnath is to use the group parameter of aes().

ggplot(data=CETW, mapping=aes(x=year, y=temp, colour=classify)) +
+ geom_point() + geom_smooth(aes(group=1))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文