如何用不同的线形,形状和颜色来绘制一个趋势图?

发布于 2025-02-10 18:34:59 字数 5703 浏览 1 评论 0 原文

我想观察FTSW的趋势。颜色由不同的集体区分,形状和线类型通过特征区分,如下图:

请参阅下面的原始数据:

df<-structure(list(Bloc = c(7, 7, 8, 8, 5, 5, 7, 7, 8, 8, 5, 5, 7, 
                            7, 8, 8, 5, 5), Pos_heliaphen = c("W16", "W17", "W36", "W37", 
                                                              "X02", "X03", "W16", "W17", "W36", "W37", "X02", "X03", "W16", 
                                                              "W17", "W36", "W37", "X02", "X03"), traitement = c("WS", "WW", 
                                                                                                                 "WW", "WS", "WS", "WW", "WS", "WW", "WW", "WS", "WS", "WW", "WS", 
                                                                                                                 "WW", "WW", "WS", "WS", "WW"), Variete = c("Blancas", "Blancas", 
                                                                                                                                                            "Blancas", "Blancas", "Blancas", "Blancas", "Blancas", "Blancas", 
                                                                                                                                                            "Blancas", "Blancas", "Blancas", "Blancas", "Blancas", "Blancas", 
                                                                                                                                                            "Blancas", "Blancas", "Blancas", "Blancas"), Date_obs = c("D07_27/05/2021", 
                                                                                                                                                                                                                      "D07_27/05/2021", "D07_27/05/2021", "D07_27/05/2021", "D07_27/05/2021", 
                                                                                                                                                                                                                      "D07_27/05/2021", "D12_07/06/2021", "D12_07/06/2021", "D12_07/06/2021", 
                                                                                                                                                                                                                      "D12_07/06/2021", "D12_07/06/2021", "D12_07/06/2021", "D13_08/06/2021", 
                                                                                                                                                                                                                      "D13_08/06/2021", "D13_08/06/2021", "D13_08/06/2021", "D13_08/06/2021", 
                                                                                                                                                                                                                      "D13_08/06/2021"), FTSW_apres_arros = c(-0.82561716279966, -0.83052784248231, 
                                                                                                                                                                                                                                                              -0.833812425397989, -0.826677385087996, -0.831991201718322, -0.827650244364889, 
                                                                                                                                                                                                                                                              0.900083501645054, 0.899646933005172, 0.920126486265779, 0.901668054319428, 
                                                                                                                                                                                                                                                              0.899616920453791, 0.899570142896103, -0.82561716279966, -0.83052784248231, 
                                                                                                                                                                                                                                                              -0.833812425397989, -0.826677385087996, -0.831991201718322, -0.827650244364889
                                                                                                                                                                                                                      )), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
                                                                                                                                                                                                                                                                                  -18L))
df %>% 
  select(1,2,3,4,5,6) %>% 
  ggplot(aes(Date_obs, FTSW_apres_arros),colour= Bloc,shape=traitement, linetype=traitement) +
  geom_point() +
  geom_line()+
  labs(y=expression(paste('FTSW')))+
  theme(legend.position="bottom", axis.text.x = element_text(angle = 90, hjust = 1))

这是我得到的数字:

如果不考虑手动编辑,我该如何将传说更改为以下图:

I want to observe the trend of FTSW. Colors are distinguished by different blocs, and shapes and line types are distinguished by traitement, like the figure below:
enter image description here

Please see the original data below:

df<-structure(list(Bloc = c(7, 7, 8, 8, 5, 5, 7, 7, 8, 8, 5, 5, 7, 
                            7, 8, 8, 5, 5), Pos_heliaphen = c("W16", "W17", "W36", "W37", 
                                                              "X02", "X03", "W16", "W17", "W36", "W37", "X02", "X03", "W16", 
                                                              "W17", "W36", "W37", "X02", "X03"), traitement = c("WS", "WW", 
                                                                                                                 "WW", "WS", "WS", "WW", "WS", "WW", "WW", "WS", "WS", "WW", "WS", 
                                                                                                                 "WW", "WW", "WS", "WS", "WW"), Variete = c("Blancas", "Blancas", 
                                                                                                                                                            "Blancas", "Blancas", "Blancas", "Blancas", "Blancas", "Blancas", 
                                                                                                                                                            "Blancas", "Blancas", "Blancas", "Blancas", "Blancas", "Blancas", 
                                                                                                                                                            "Blancas", "Blancas", "Blancas", "Blancas"), Date_obs = c("D07_27/05/2021", 
                                                                                                                                                                                                                      "D07_27/05/2021", "D07_27/05/2021", "D07_27/05/2021", "D07_27/05/2021", 
                                                                                                                                                                                                                      "D07_27/05/2021", "D12_07/06/2021", "D12_07/06/2021", "D12_07/06/2021", 
                                                                                                                                                                                                                      "D12_07/06/2021", "D12_07/06/2021", "D12_07/06/2021", "D13_08/06/2021", 
                                                                                                                                                                                                                      "D13_08/06/2021", "D13_08/06/2021", "D13_08/06/2021", "D13_08/06/2021", 
                                                                                                                                                                                                                      "D13_08/06/2021"), FTSW_apres_arros = c(-0.82561716279966, -0.83052784248231, 
                                                                                                                                                                                                                                                              -0.833812425397989, -0.826677385087996, -0.831991201718322, -0.827650244364889, 
                                                                                                                                                                                                                                                              0.900083501645054, 0.899646933005172, 0.920126486265779, 0.901668054319428, 
                                                                                                                                                                                                                                                              0.899616920453791, 0.899570142896103, -0.82561716279966, -0.83052784248231, 
                                                                                                                                                                                                                                                              -0.833812425397989, -0.826677385087996, -0.831991201718322, -0.827650244364889
                                                                                                                                                                                                                      )), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
                                                                                                                                                                                                                                                                                  -18L))
df %>% 
  select(1,2,3,4,5,6) %>% 
  ggplot(aes(Date_obs, FTSW_apres_arros),colour= Bloc,shape=traitement, linetype=traitement) +
  geom_point() +
  geom_line()+
  labs(y=expression(paste('FTSW')))+
  theme(legend.position="bottom", axis.text.x = element_text(angle = 90, hjust = 1))

And here is the figure I got:
enter image description here

If not considering the manual editing, how can I change the legend to the figure belowing:
enter image description here

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

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

发布评论

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

评论(1

北城挽邺 2025-02-17 18:34:59

代码的一个问题是,将 aes()的闭合括号放在错误的位置,以便 aes()()中不包含颜色,形状和线型。另外,由于您的 date_obs 列是您必须 group by Eg 互动(BLOC,TRAITEMT)以获取行绘图:

library(ggplot2)
library(dplyr)

df %>%
  select(1, 2, 3, 4, 5, 6) %>%
  ggplot(aes(Date_obs, FTSW_apres_arros, colour = factor(Bloc), shape = traitement, 
             linetype = traitement, group = interaction(Bloc, traitement))) +
  geom_point() +
  geom_line() +
  labs(y = expression(paste("FTSW"))) +
  theme(legend.position = "bottom", axis.text.x = element_text(angle = 90, hjust = 1))

您可以从数据集中创建标签向量

编辑 gude_legend(order = 1),以便首先出现颜色传说。

library(ggplot2)
library(dplyr)

labels <- df |> 
  select(Bloc, Pos_heliaphen) |> 
  distinct(Bloc, Pos_heliaphen) |> 
  group_by(Bloc) |> 
  summarise(Pos_heliaphen = paste(Pos_heliaphen, collapse = "-")) |> 
  tibble::deframe()

df %>%
  select(1, 2, 3, 4, 5, 6) %>%
  ggplot(aes(Date_obs, FTSW_apres_arros, colour = factor(Bloc), shape = traitement, linetype = traitement, group = interaction(Bloc, traitement))) +
  geom_point() +
  geom_line() +
  scale_color_discrete(labels = labels, guide = guide_legend(order = 1)) +
  labs(y = expression(paste("FTSW"))) +
  theme(legend.position = "bottom", 
        axis.text.x = element_text(angle = 90, hjust = 1))

One issue with your code is that you put the closing parenthesis for aes() at the wrong position so that color, shape and linetype were not included in aes(). Also, as your Date_obs column is a categorical you have to group by e.g. interaction(Bloc, traitement) to get a line plot:

library(ggplot2)
library(dplyr)

df %>%
  select(1, 2, 3, 4, 5, 6) %>%
  ggplot(aes(Date_obs, FTSW_apres_arros, colour = factor(Bloc), shape = traitement, 
             linetype = traitement, group = interaction(Bloc, traitement))) +
  geom_point() +
  geom_line() +
  labs(y = expression(paste("FTSW"))) +
  theme(legend.position = "bottom", axis.text.x = element_text(angle = 90, hjust = 1))

enter image description here

EDIT You could create a vector of labels from your dataset which you could pass to scale_color_discrete like so:

Note: I also added guide = guide_legend(order = 1) so that the color legend comes first.

library(ggplot2)
library(dplyr)

labels <- df |> 
  select(Bloc, Pos_heliaphen) |> 
  distinct(Bloc, Pos_heliaphen) |> 
  group_by(Bloc) |> 
  summarise(Pos_heliaphen = paste(Pos_heliaphen, collapse = "-")) |> 
  tibble::deframe()

df %>%
  select(1, 2, 3, 4, 5, 6) %>%
  ggplot(aes(Date_obs, FTSW_apres_arros, colour = factor(Bloc), shape = traitement, linetype = traitement, group = interaction(Bloc, traitement))) +
  geom_point() +
  geom_line() +
  scale_color_discrete(labels = labels, guide = guide_legend(order = 1)) +
  labs(y = expression(paste("FTSW"))) +
  theme(legend.position = "bottom", 
        axis.text.x = element_text(angle = 90, hjust = 1))

enter image description here

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