如何取平均值并制作虚线图,并将折线更改为平滑曲线?并添加图例?

How to take the average of the values and make a dotted line graph, and change the polyline to a smooth curve? And add the legend?

我试着取 SF_Plante_VerteSF_Plante_Totale 的平均值]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))

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

任何人都可以帮助解决我的问题吗?提前致谢!

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

这里有一些问题。

  1. 您没有得到图例,因为您手动为 aes() 之外的每个图层指定了颜色。 {ggplot2} 不会自动为 aes().
  2. 之外指定的事物创建图例
  3. 您无法将不同的组作为单个变量访问以在 aes() 中提供,因为您的数据当前采用 'wide' 格式,其中您有多个列包含相同类型的数据和列名是区分这些测量的简单元数据。解决办法是转成'long'格式。为此,我使用 tidyr::pivot_longer().
  4. 要获得您想要的颜色,您只需使用 scale_color_manual()
  5. 如果你想拟合一条平滑的曲线而不只是一条 point-to-point 线,你可以使用 loess 平滑并简单地调整 span 参数来控制平滑或颠簸的程度是。
  6. 您只需更改 linetype 即可获得 dotted/dashed 行。有关控制 linetype.
  7. 的可用选项的更多信息,请参阅 here
  8. 如果您想分别可视化来自不同 Pos_heliaphen 组的数据,这将有助于添加另一种美学来区分它们。例如,您可以使用 shape 来区分点,使用 linetype 来区分平滑线。
  9. 我使用 interaction() 创建了分组变量的所有组合。
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 package (v2.0.1)

创建于 2022-04-07