Home > Enterprise >  How can I plot one trend figure with different aes of linetype, shape and color?
How can I plot one trend figure with different aes of linetype, shape and color?

Time:06-28

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

CodePudding user response:

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

  • Related