0

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.

This is how my plot look like now

This is how I want my plot to look

How can I do this? Thank you so much!

3
  • please edit your question to include the data as code. Do not expect people to create the dataframe in R cell by cell from your image. Commented Sep 22, 2021 at 1:58
  • for example: dat <- data.frame(day = c(1,2,3), group = c(4,5,6)) Commented Sep 22, 2021 at 2:00
  • 1
    Hi and welcome @Jason to stack overflow. It seems that a simliar question has been answered here: stackoverflow.com/questions/26039119/… . The solution could be something like stat_summary(aes(group=1), fun.y=mean, colour="red", geom="line",group=1) Commented Sep 22, 2021 at 2:07

1 Answer 1

0
#Libraries

library(tidyverse)

#Data

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))


#How to

dat %>% 
  ggplot(aes(x = factor(day), y = score)) +
  geom_boxplot(aes(fill = group))+
  geom_line(
    data = dat %>% 
      group_by(day) %>% 
      summarise(score = median(score,na.rm = TRUE)),
    aes(group = 1),
    size = 1,
    col = "red"
  )

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.