如何取平均值并制作虚线图,然后将多线线更改为平滑曲线?并添加传奇?

发布于 2025-01-19 14:51:34 字数 6208 浏览 4 评论 0原文

我尝试根据 Date_obsSF_Plante_VerteSF_Plante_Totale 的平均值。

df<-structure(list(Pos_heliaphen = c("X47", "W17", "Z17", "X47", 
                                     "Y19", "Y40", "X47", "Y19", "Y40", "Z17", "Z31", "X47", "Y19", 
                                     "Y40", "Z31", "X47", "Z17", "Z31"), traitement = c("WW", "WW", 
                                                                                        "WW", "WW", "WW", "WW", "WW", "WW", "WW", "WW", "WW", "WW", "WW", 
                                                                                        "WW", "WW", "WW", "WW", "WW"), Variete = c("Blancas", "Blancas", 
                                                                                                                                   "Blancas", "Blancas", "Blancas", "Blancas", "Blancas", "Blancas", 
                                                                                                                                   "Blancas", "Blancas", "Blancas", "Blancas", "Blancas", "Blancas", 
                                                                                                                                   "Blancas", "Blancas", "Blancas", "Blancas"), Date_obs = c("D11_04/06/2021", 
                                                                                                                                                                                             "D11_04/06/2021", "D11_04/06/2021", "D12_07/06/2021", "D12_07/06/2021", 
                                                                                                                                                                                             "D12_07/06/2021", "D23_25/06/2021", "D23_25/06/2021", "D23_25/06/2021", 
                                                                                                                                                                                             "D23_25/06/2021", "D23_25/06/2021", "D24_28/06/2021", "D24_28/06/2021", 
                                                                                                                                                                                             "D24_28/06/2021", "D24_28/06/2021", "D25_29/06/2021", "D25_29/06/2021", 
                                                                                                                                                                                             "D25_29/06/2021"), SF_Plante_Totale = c(46473, 44589.3, 43134, 
                                                                                                                                                                                                                                     166645.5, 119962.5, 93061.5, 483583.8, 313985.7, 273432.6, 414871.8, 
                                                                                                                                                                                                                                     426766.2, 539410.2, 337417.5, 273432.6, 474915, 539410.2, 414871.8, 
                                                                                                                                                                                                                                     474915), SF_Plante_Verte = c(46473, 44589.3, 43134, 162512.7, 
                                                                                                                                                                                                                                                                  119962.5, 93061.5, 462655.2, 293367.9, 238373.1, 363123.6, 407572.2, 
                                                                                                                                                                                                                                                                  473793.6, 316799.7, 238373.1, 420682.5, 473793.6, 363123.6, 420682.5
                                                                                                                                                                                                                                     ), SF_Plante_senescence = c(0, 0, 0, 4132.8, 0, 0, 20928.6, 20617.8, 
                                                                                                                                                                                                                                                                 35059.5, 51748.2, 19194, 65616.6, 20617.8, 35059.5, 54232.5, 
                                                                                                                                                                                                                                                                 65616.6, 51748.2, 54232.5)), class = c("tbl_df", "tbl", "data.frame"
                                                                                                                                                                                                                                                                 ), row.names = c(NA, -18L))

使用下面的代码,我想绘制一条虚线,但我想获得平滑的曲线而不是折线(没有折线段)。而且我也无法成功添加图例。

有人可以帮助解决我的问题吗?先感谢您!

ggplot(df, aes(x = Date_obs)) + 
  stat_summary(aes(y = SF_Plante_Totale,group=1), fun =mean, colour="white",shape=21,size=4,fill="steelblue",geom="point",group=1)+
  stat_summary(aes(y = SF_Plante_Totale,group=1), fun =mean,colour="steelblue", geom="smooth", group=1)+
  stat_summary(aes(y = SF_Plante_Verte,group=1), fun =mean, colour="white",shape=21,size=4,fill="tomato",geom="point",group=2)+
  stat_summary(aes(y = SF_Plante_Verte,group=1), fun =mean,colour="tomato", geom="smooth", group=1)+
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

输入图片此处描述

I tried to take the average of SF_Plante_Verte and SF_Plante_Totale acccording to their Date_obs.

df<-structure(list(Pos_heliaphen = c("X47", "W17", "Z17", "X47", 
                                     "Y19", "Y40", "X47", "Y19", "Y40", "Z17", "Z31", "X47", "Y19", 
                                     "Y40", "Z31", "X47", "Z17", "Z31"), traitement = c("WW", "WW", 
                                                                                        "WW", "WW", "WW", "WW", "WW", "WW", "WW", "WW", "WW", "WW", "WW", 
                                                                                        "WW", "WW", "WW", "WW", "WW"), Variete = c("Blancas", "Blancas", 
                                                                                                                                   "Blancas", "Blancas", "Blancas", "Blancas", "Blancas", "Blancas", 
                                                                                                                                   "Blancas", "Blancas", "Blancas", "Blancas", "Blancas", "Blancas", 
                                                                                                                                   "Blancas", "Blancas", "Blancas", "Blancas"), Date_obs = c("D11_04/06/2021", 
                                                                                                                                                                                             "D11_04/06/2021", "D11_04/06/2021", "D12_07/06/2021", "D12_07/06/2021", 
                                                                                                                                                                                             "D12_07/06/2021", "D23_25/06/2021", "D23_25/06/2021", "D23_25/06/2021", 
                                                                                                                                                                                             "D23_25/06/2021", "D23_25/06/2021", "D24_28/06/2021", "D24_28/06/2021", 
                                                                                                                                                                                             "D24_28/06/2021", "D24_28/06/2021", "D25_29/06/2021", "D25_29/06/2021", 
                                                                                                                                                                                             "D25_29/06/2021"), SF_Plante_Totale = c(46473, 44589.3, 43134, 
                                                                                                                                                                                                                                     166645.5, 119962.5, 93061.5, 483583.8, 313985.7, 273432.6, 414871.8, 
                                                                                                                                                                                                                                     426766.2, 539410.2, 337417.5, 273432.6, 474915, 539410.2, 414871.8, 
                                                                                                                                                                                                                                     474915), SF_Plante_Verte = c(46473, 44589.3, 43134, 162512.7, 
                                                                                                                                                                                                                                                                  119962.5, 93061.5, 462655.2, 293367.9, 238373.1, 363123.6, 407572.2, 
                                                                                                                                                                                                                                                                  473793.6, 316799.7, 238373.1, 420682.5, 473793.6, 363123.6, 420682.5
                                                                                                                                                                                                                                     ), SF_Plante_senescence = c(0, 0, 0, 4132.8, 0, 0, 20928.6, 20617.8, 
                                                                                                                                                                                                                                                                 35059.5, 51748.2, 19194, 65616.6, 20617.8, 35059.5, 54232.5, 
                                                                                                                                                                                                                                                                 65616.6, 51748.2, 54232.5)), class = c("tbl_df", "tbl", "data.frame"
                                                                                                                                                                                                                                                                 ), row.names = c(NA, -18L))

With the code below, I want to draw a dotted line, but I want to get smooth curves instead of polylines (no polyline segments).And I can't add legend successfully either.

Could anyone please help save my problem? Thank you in advance!

ggplot(df, aes(x = Date_obs)) + 
  stat_summary(aes(y = SF_Plante_Totale,group=1), fun =mean, colour="white",shape=21,size=4,fill="steelblue",geom="point",group=1)+
  stat_summary(aes(y = SF_Plante_Totale,group=1), fun =mean,colour="steelblue", geom="smooth", group=1)+
  stat_summary(aes(y = SF_Plante_Verte,group=1), fun =mean, colour="white",shape=21,size=4,fill="tomato",geom="point",group=2)+
  stat_summary(aes(y = SF_Plante_Verte,group=1), fun =mean,colour="tomato", geom="smooth", group=1)+
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

enter image description here

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

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

发布评论

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

评论(1

鱼忆七猫命九 2025-01-26 14:51:34

这里有一些问题。

  1. 您不会获得传奇,因为您将颜色指定到aes()之外的每一层。 {ggplot2}不会自动为在aes()()之外指定的事物的传说。
  2. 您无法访问其他组作为单个变量来提供aes()中的提供是简单的元数据来区分这些测量值。解决方案是转换为“长”格式。为此,我使用tidyr :: pivot_longer()
  3. 要获得所需的颜色,您只需使用scale_color_manual()即可。
  4. 如果要拟合平滑的曲线,而不仅仅是点对点线,可以使用loess平滑,只需调整span参数即可控制如何光滑或颠簸这是。
  5. 您只需更改lineType以获取虚线/虚线即可。请参阅在这里有关可用选项的更多信息,以控制linetype
  6. 如果要从不同的pos_heliaphen组分别可视化数据,则将有助于添加另一种美学以区分它们。例如,您可以使用shape来区分点和lineType以区分平滑线。
  7. 我使用互动()来创建分组变量的所有组合。
library(tidyverse)

df %>% 
  select(1, 4:6) %>% 
  pivot_longer(starts_with("SF")) %>% 
  ggplot(aes(Date_obs, value, color = name, group = interaction(name, Pos_heliaphen))) +
  geom_point(stat = "summary", size = 4, aes(shape = Pos_heliaphen)) +
  geom_smooth(method = "loess", se = F, span = 5, aes(linetype = Pos_heliaphen)) +
  scale_color_manual(values = c("steelblue", "tomato")) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

“”

reprex软件包(v2.0.1)

There are a few issues here.

  1. You don't get a legend because you specify the color manually to each layer outside of aes(). {ggplot2} won't automatically make a legend for things specified outside of aes().
  2. You can't access the different groups as a single variable to provide in aes() because your data is currently in 'wide' format where you have multiple columns containing the same type of data and the column names are simple metadata distinguishing those measurements. The solution is to convert to 'long' format. For this, I use tidyr::pivot_longer().
  3. To get your desired colors you can just use scale_color_manual().
  4. If you want to fit a smoothed curve rather than just a point-to-point line, you can use loess smoothing and simply adjust the span parameter to control how smooth or bumpy it is.
  5. You can just change the linetype to get a dotted/dashed line. See here for more info on available options to control linetype.
  6. If you want to separately visualize data from different Pos_heliaphen groups, it will help to add another aesthetic to distinguish them. For example you could use shape to distinguish the points and linetype to distinguish the smoothed lines.
  7. I used interaction() to create all the combinations of the grouping variables.
library(tidyverse)

df %>% 
  select(1, 4:6) %>% 
  pivot_longer(starts_with("SF")) %>% 
  ggplot(aes(Date_obs, value, color = name, group = interaction(name, Pos_heliaphen))) +
  geom_point(stat = "summary", size = 4, aes(shape = Pos_heliaphen)) +
  geom_smooth(method = "loess", se = F, span = 5, aes(linetype = Pos_heliaphen)) +
  scale_color_manual(values = c("steelblue", "tomato")) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Created on 2022-04-07 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文