0

I am trying to do a very simple geom plot but it is becoming complex due to following reasons. I have two variables Date and Condition. Their data type is Date and Char respectively. Following data exist in it.

Date Condition
2015-11-26 zoo1
2022-01-14 K621
2020-01-14 K20
2021-01-14 G341
2025-01-14 F21
2025-01-14 G309 D

I have total 83742 entries for the above example table. I am trying to find how much are the total MAIN entries by each year and by each month. i-e, I want to generate two separate graphs by Month and by Year which can show the total number of conditions by each month or by each year. Thanks

MrFlick
  • 195,160
  • 17
  • 277
  • 295
Usman YousafZai
  • 1,088
  • 4
  • 18
  • 44

1 Answers1

0

I use the lubridate library's round_date() function for this type of problem:

library(lubridate)
df %>% 
  mutate(YearMonth=round_date(Date, "month"),
         Year=round_date(Date, "year")) %>% 
  ggplot(aes(x=YearMonth, fill=Condition))+
  geom_bar()

df %>% 
    mutate(YearMonth=round_date(Date, "month"),
           Year=round_date(Date, "year")) %>% 
    ggplot(aes(x=Year, fill=Condition)) +
    geom_bar() +
    scale_x_date(date_breaks = "1 year", date_labels = "%Y")
M.Viking
  • 5,067
  • 4
  • 17
  • 33