r ggplot barplot with multiple date columns

Viewed 100

I have a data frame with multiple date columns and I want to make a single plot with 3 bar charts (one for ID/dat1, ID/dat2 and ID/dat3). Anyone know how to do this?

EDIT: I'm looking for a plot with the date on the x-axis and count of ID on the y-axis.

Example data frame:

dat <- data.frame(ID = c(1:80),
                     dat1 = sample(seq(as.Date('2021/01/01'), as.Date('2021/04/01'), by="day"), 80),
                     dat2 = sample(seq(as.Date('2021/01/01'), as.Date('2021/04/01'), by="day"), 80),
                     dat3 = sample(seq(as.Date('2021/01/01'), as.Date('2021/04/01'), by="day"), 80))
1 Answers

Are you after this?

melt(setDT(dat), id.vars = "ID") %>%
  ggplot(aes(x = value, fill = variable)) +
  geom_bar()

enter image description here


If you want to have line plot, you can try

melt(setDT(dat),id.vars = "ID") %>% 
  ggplot(aes(x = value, y = ID, group = variable, color = variable)) + 
  geom_line()

enter image description here

Related