Home > database >  How can I make the histogram timeline follow the factual time instead of the numeric value of the da
How can I make the histogram timeline follow the factual time instead of the numeric value of the da

Time:09-14

I'm trying to plot a histogram by factual time, but the dates on the x-axis are arranged by the numeric value of the date.

Here is the original data:

cd<-structure(list(Bloc = c(7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
                        7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7), Pos_heliaphen = c("W16", 
                                                                                              "W17", "W16", "W17", "W16", "W17", "W16", "W17", "W16", "W17", 
                                                                                              "W16", "W17", "W16", "W17", "W16", "W17", "W16", "W17", "W16", 
                                                                                              "W17", "W16", "W17", "W16", "W17", "W16", "W17", "W16", "W17", 
                                                                                              "W16", "W17"), traitement = c("WS", "WW", "WS", "WW", "WS", "WW", 
                                                                                                                            "WS", "WW", "WS", "WW", "WS", "WW", "WS", "WW", "WS", "WW", "WS", 
                                                                                                                            "WW", "WS", "WW", "WS", "WW", "WS", "WW", "WS", "WW", "WS", "WW", 
                                                                                                                            "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", "Blancas", "Blancas", "Blancas", "Blancas", 
                                                                                                                                                     "Blancas", "Blancas"), Date_obs = c("26/05/2021", "26/05/2021", 
                                                                                                                                                                                         "27/05/2021", "27/05/2021", "04/06/2021", "04/06/2021", "07/06/2021", 
                                                                                                                                                                                         "07/06/2021", "08/06/2021", "08/06/2021", "11/06/2021", "11/06/2021", 
                                                                                                                                                                                         "14/06/2021", "14/06/2021", "15/06/2021", "15/06/2021", "16/06/2021", 
                                                                                                                                                                                         "16/06/2021", "18/06/2021", "18/06/2021", "21/06/2021", "21/06/2021", 
                                                                                                                                                                                         "23/06/2021", "23/06/2021", "25/06/2021", "25/06/2021", "28/06/2021", 
                                                                                                                                                                                         "28/06/2021", "29/06/2021", "29/06/2021"), SF_tot = c(NA, NA, 
                                                                                                                                                                                                                                               43990.8, 44589.3, NA, NA, 139515.6, 147384.3, NA, NA, 187836.6, 
                                                                                                                                                                                                                                               198513, 234408.3, 249490.5, NA, NA, 256949.7, 296826.6, 260007.3, 
                                                                                                                                                                                                                                               337029, 260007.3, 389098.5, 260007.3, 418418.7, 260007.3, 446741.4, 
                                                                                                                                                                                                                                               260007.3, 446741.4, 260007.3, 446741.4)), class = c("tbl_df", 
                                                                                                                                                                                                                                                                                                   "tbl", "data.frame"), row.names = c(NA, -30L))

And here is the code I use (it looks cumbersome because my raw data is much more than presented above):

library(ggplot2)
library(dplyr)
library(readxl)
library(tidyverse)

df<-subset(cd, Variete %in% c("Blancas"))
options(scipen=200)

df <- df %>% 
  select(1,2,3,4,5,6) %>% 
  pivot_longer(starts_with("SF"))

df2 <- df %>%
  group_by(Bloc, Date_obs) %>%
  summarize(ww = paste0(Pos_heliaphen, collapse = "-"))

df %>%
  left_join(df2, by = c("Bloc", "Date_obs")) %>%
  ggplot(aes(Date_obs, value,group=traitement, fill = traitement))  
  geom_bar(stat = "identity", position = "dodge")  
  scale_fill_manual(values = c("#8FBC94","#548687")) 
  facet_wrap(~ww,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())   
  labs(title = "Blancas")

Here is the figure I got: enter image description here

The dates of May (26/05/2021,27/05/2021) should have been presented in the beginning when following the factual date. And I hope the missing date can be expressed as well, for example there should be an interval between 16/08/2021 and 18/08/2021 which represents one day.

Any suggestions are welcome! Thank you in advance!!!

CodePudding user response:

You could achieve your desired result by converting you date column to a proper data using as.Date(). Afterwards you could set the number of breaks and/or the format of the date labels via scale_x_date. In my code below I added a break for each day which however is probably to much. (;

df %>%
  left_join(df2, by = c("Bloc", "Date_obs")) %>%
  ggplot(aes(as.Date(Date_obs, format = "%d/%m/%Y"), value, group = traitement, fill = traitement))  
  geom_bar(stat = "identity", position = "dodge")  
  scale_x_date(date_breaks = "1 day", date_labels = "%d/%m/%Y")  
  scale_fill_manual(values = c("#8FBC94", "#548687"))  
  facet_wrap(~ww, 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())  
  labs(title = "Blancas")
#> Warning: Removed 8 rows containing missing values (geom_bar).

![](https://i.imgur.com/4AxD9Pv.png)

  • Related