Home > Software design >  How create a box plot line plot in a single plot using ggplot2
How create a box plot line plot in a single plot using ggplot2

Time:09-23

I want to create a box plot line plot in a single plot using ggplot2

This is what my code now:

library(ggplot2)
dat <- data.frame(day = c(0,0,0,0,0,0,10,10,10,10,10,10,14,14,14,14,14,14,21,21,21,21,21,21,28,28,28,28,28,28,35,35,35,35,35,35,42,42,42,42,42,42), group = c('Saline','RP','Saline','Saline','RP','RP','Saline','RP','Saline','Saline','RP','RP','Saline','RP','Saline','Saline','RP','RP','Saline','RP','Saline','Saline','RP','RP','Saline','RP','Saline','Saline','RP','RP','Saline','RP','Saline','Saline','RP','RP','Saline','RP','Saline','Saline','RP','RP'), score = c(37.5,43,7,63,26,15,17,16,43,26,53,26,26,26,43,10,6,15,18,9,10,4,8,18,60,26,20,12.5,9,43,43,43,11,10,7,60,43,43,32,10.5,8,57.5))


g1 = ggplot(data = dat, aes(x = factor(day), y = score))  
  geom_boxplot(aes(fill = group))

g1

When doing box plot, I want scores of different treatments(groups) to be represented separately, so I let x = factor(day). But for line plot, I want each day's score to be the average of the two treatments(group) of the day.

enter image description here

  • Related