Home > OS >  How to plot two variables of the same day in the same line?
How to plot two variables of the same day in the same line?

Time:07-22

I want to plot two variables of the same date in a same line. If it is the same date, the two variables will be displayed on the same abscissa. But I want to keep them separate without affecting the coherence of the abscissa.

Here is the original code:

df<-structure(list(Bloc = c(7, 7, 8, 8, 5, 5, 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", "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", "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", "Blancas", "Blancas", "Blancas", "Blancas", "Blancas", 
                               "Blancas"), Date_obs = c("D1_27/05/2021", "D1_27/05/2021", 
                                                        "D1_27/05/2021", "D1_27/05/2021", "D1_27/05/2021", "D1_27/05/2021", 
                                                        "D2_28/05/2021", "D2_28/05/2021", "D2_28/05/2021", "D2_28/05/2021", 
                                                        "D2_28/05/2021", "D2_28/05/2021", "D3_29/05/2021", "D3_29/05/2021", 
                                                        "D3_29/05/2021", "D3_29/05/2021", "D3_29/05/2021", "D3_29/05/2021", 
                                                        "D4_30/05/2021", "D4_30/05/2021", "D4_30/05/2021", "D4_30/05/2021", 
                                                        "D4_30/05/2021", "D4_30/05/2021"), P_av_arros = c(0.51, 0.53, 
                                                                                                          0.55, 0.57, 0.59, 0.61, 0.63, 0.65, 0.67, 0.69, 0.71, 0.73, 
                                                                                                          0.75, 0.77, 0.79, 0.81, 0.83, 0.85, 0.87, 0.89, 0.91, 0.93, 
                                                                                                          0.95, 0.97), FTSW_apres_arros = c(0.91, 0.92, 0.93, 0.94, 
                                                                                                                                            0.95, 0.96, 0.97, 0.98, 0.99, 1, 1.01, 1.02, 1.03, 1.04, 
                                                                                                                                            1.05, 1.06, 1.07, 1.08, 1.09, 1.1, 1.11, 1.12, 1.13, 1.14
                                                                                                          )), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
                                                                                                                                                                      -24L))

I've already tried with the code:

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

And I got this graph: enter image description here

I can also the figure below if I change FTSW_apres_arros to P_av_arros: enter image description here

Now I want to plot the two variables FTSW_apres_arros and P_av_arrosin in the same line without changing the original excel format. Below is the figure that I hope I can have: enter image description here

I'm not sure if it's possible?

CodePudding user response:

You could do:

df %>%
  mutate(Date_obs = as.POSIXct(lubridate::dmy(Date_obs))) %>%
  tidyr::pivot_longer(6:7) %>%
  mutate(Date_obs = if_else(name == "FTSW_apres_arros", 
                            Date_obs   43200, Date_obs)) %>%
  ggplot(aes(Date_obs, value, 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))  
  scale_x_datetime(date_labels = "%d/%m/%Y", date_breaks = "day")  
  labs(y = expression(paste("FTSW")))  
  theme(legend.position = "bottom", 
        axis.text.x = element_text(angle = 90, hjust = 1))

enter image description here

  • Related