Home > Back-end >  How can I plot a histogram of comparisons over time?
How can I plot a histogram of comparisons over time?

Time:06-07

I need to observe the leaf area under the two treatments (WW and WS) for a long time, so I want to draw a histogram comparison chart over time, but I don't know how to put the two bars of two treatments in one graph.

The data is as follows:

cd<-structure(list(Pos_heliaphen = c("Y12", "Y13", "Y20", "Y21", 
                                     "Y34", "Y35", "Y42", "Y43", "Z06", "Z07", "Z22", "Z23", "Z36", 
                                     "Z37", "Z44", "Z45", "Y12", "Y13", "Y20", "Y21", "Y34", "Y35", 
                                     "Y42", "Y43", "Z06", "Z07", "Z22", "Z23", "Z36", "Z37", "Z44", 
                                     "Z45"), traitement = c("WW", "WS", "WS", "WW", "WS", "WW", "WS", 
                                                            "WW", "WW", "WS", "WS", "WW", "WS", "WW", "WW", "WS", "WW", "WS", 
                                                            "WS", "WW", "WS", "WW", "WS", "WW", "WW", "WS", "WS", "WW", "WS", 
                                                            "WW", "WW", "WS"), Variete = c("Angelica", "Angelica", "Angelica", 
                                                                                           "Angelica", "Angelica", "Angelica", "Angelica", "Angelica", "Angelica", 
                                                                                           "Angelica", "Angelica", "Angelica", "Angelica", "Angelica", "Angelica", 
                                                                                           "Angelica", "Angelica", "Angelica", "Angelica", "Angelica", "Angelica", 
                                                                                           "Angelica", "Angelica", "Angelica", "Angelica", "Angelica", "Angelica", 
                                                                                           "Angelica", "Angelica", "Angelica", "Angelica", "Angelica"), 
                   Date_obs = structure(c(19135, 19135, 19135, 19135, 19135, 
                                          19135, 19135, 19135, 19135, 19135, 19135, 19135, 19135, 19135, 
                                          19135, 19135, 19145, 19145, 19145, 19145, 19145, 19145, 19145, 
                                          19145, 19145, 19145, 19145, 19145, 19145, 19145, 19145, 19145
                   ), class = "Date"), SF_Plante_Totale = c(0, 0, 0, 0, 0, 0, 
                                                            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67589.64, 71868.33, 69883.2, 
                                                            73892.79, 72106.38, 82228.68, 73393.92, 63867.78, 70127.46, 
                                                            64710.27, 76991.58, 75486.69, 80237.34, 74250.9, 68804.73, 
                                                            63714.6)), row.names = c(NA, -32L), class = c("tbl_df", "tbl", 
                                                                                                          "data.frame"))

The code I used is as follows (There are redundant operations because my original data has more "variete"):

df<-subset(cd, Variete %in% c("Angelica"))
options(scipen=200)
df$Date_obs <- as.Date(df$Date_obs)



df %>% 
  select(1,2,3,4,5) %>% 
  pivot_longer(starts_with("SF")) %>% 
  ggplot(aes(Date_obs, value,group=Pos_heliaphen))  
  geom_bar(stat = "identity",fill="steelblue")  
  scale_x_date(date_labels = "%Y-%m-%d") 
  facet_wrap(~Pos_heliaphen,drop=TRUE) 
  labs(y=expression(paste('Original total leaf area (mm'^2,')'))) 
  theme(legend.position="bottom", axis.text.x = element_text(angle = 90, hjust = 1), axis.title.x = element_blank())

And I got the figure as below:

enter image description here

But I want to compare the two bars circled together in one graph (One is WW, the other is WS). Like this graph below. Then only 8 graphs left in the end.

enter image description here

Because right now is just the beginning of observation, so we don't really have time line. But the two figures below are from last year. I just don't know How to put the WS and WW of the same day in one figure. enter image description here enter image description here

CodePudding user response:

I'm not sure this is what you wanted but

df %>% 
  select(1,2,3,4,5) %>% 
  pivot_longer(starts_with("SF")) %>%
  mutate(w = substr(Pos_heliaphen, start = 1, stop = 2)) %>%
  ggplot(aes(Date_obs, value,group=traitement, fill = traitement))  
  geom_bar(stat = "identity", position = "dodge")  
  scale_x_date(date_labels = "%Y-%m-%d") 
  facet_wrap(~w,drop=TRUE) 
  labs(y=expression(paste('Original total leaf area (mm'^2,')'))) 
  theme(legend.position="bottom", axis.text.x = element_text(angle = 90, hjust = 1), axis.title.x = element_blank())

enter image description here

New

df %>% 
  select(1,2,3,4,5) %>% 
  pivot_longer(starts_with("SF")) %>%
  mutate(w = substr(Pos_heliaphen, start = 1, stop = 2)) %>%
  group_by(Date_obs, w) %>%
  filter(!(sum(value) == 0)) %>%
  ggplot(aes(Date_obs, value,group=traitement, fill = traitement))  
  geom_bar(stat = "identity", position = "dodge")  
  geom_text(aes(label = Pos_heliaphen), position=position_dodge(width=0.9), size = 2, vjust = -0.5)  
  ylim(c(0, 90000))  
  scale_x_date(date_labels = "%Y-%m-%d") 
  facet_wrap(~w,drop=TRUE) 
  labs(y=expression(paste('Original total leaf area (mm'^2,')'))) 
  theme(legend.position="bottom", axis.text.x = element_text(angle = 45, hjust = 1), axis.title.x = element_blank())   
  theme(
    strip.background = element_blank(),
    strip.text.x = element_blank()
  )  
  labs(title = "Angelica")

enter image description here

  • Related