LDT
LDT

Reputation: 3108

Plot boxplots over time using multiple categories

I am sorry for the header I was not so sure how to ask about it. I have a data frame that looks like this.

Sample=c("A","A", "A", "B","B","B","A","A", "A", "B","B","B","A","A", "A", "B","B","B","A","A", "A", "B","B","B")
Treatment=c("twiter","twiter","twiter","twiter","twiter","twiter","facebook","facebook","facebook","facebook","facebook","facebook",
            "twiter","twiter","twiter","twiter","twiter","twiter","facebook","facebook","facebook","facebook","facebook","facebook")
replicate=c(1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3)
time=c( 10,10,10,10,10,10,10,10,10,10,10,10,20,20,20,20,20,20,20,20,20,20,20,20)
points=c(20,40,80,20,60,120, 30,100,55, 28, 45,90, 80,20,100, 40,90,56,20,30,12,3,5,8)
length(points)

   Sample Treatment replicate time points
1       A    twiter         1   10     20
2       A    twiter         2   10     40
3       A    twiter         3   10     80
4       B    twiter         1   10     20
5       B    twiter         2   10     60
6       B    twiter         3   10    120
7       A  facebook         1   10     30
8       A  facebook         2   10    100
9       A  facebook         3   10     55
10      B  facebook         1   10     28
11      B  facebook         2   10     45
12      B  facebook         3   10     90
13      A    twiter         1   20     80
14      A    twiter         2   20     20
15      A    twiter         3   20    100
16      B    twiter         1   20     40
17      B    twiter         2   20     90
18      B    twiter         3   20     56
19      A  facebook         1   20     20
20      A  facebook         2   20     30
21      A  facebook         3   20     12
22      B  facebook         1   20      3
23      B  facebook         2   20      5
24      B  facebook         3   20      8

I would like to plot my data using boxplots at each time point. I would like to have one box plot that shows Sample A with "twiter" Sample A with "facebook" Sample "B" with "twiter" and Sample B with "facebook" at time point 10 and the same at time point 20.

So far I can do something like this. enter image description here

ggplot(data,aes(x=time, y=points,color=Sample, fill=Sample, group=interaction(Sample,Treatment)), alpha=0.1) +
  geom_boxplot(alpha=0.1) +
  geom_point(position = position_dodge(width=0.75), alpha=0.2)+
  theme_bw() 

But this is wrong I would like to have the sample A, and B from the two different treatments next to each other at each time point to have a look at the differences. I don't want to use facet_wrap. It is a challenge for me. Thank you for your time

Upvotes: 2

Views: 1401

Answers (3)

Gregor Thomas
Gregor Thomas

Reputation: 145965

Turning my comment into an answer: your issue is that group=interaction(Sample,Treatment) overrides the grouping by the x-axis (time) that would normally be done. To include time in the grouping, add it to the interaction:

ggplot(data,
       aes(
         x = time,
         y = points,
         color = Sample,
         fill = Sample,
         group = interaction(Sample, Treatment, time)
       ),
       alpha = 0.1) +
  geom_boxplot(alpha = 0.1) +
  geom_point(position = position_dodge(width = 0.75), alpha = 0.2) +
  theme_bw()

enter image description here

Of course, the issue remains that there's no way to tell which box goes with which treatment, but I'll leave that to you to address.

Upvotes: 2

jmar
jmar

Reputation: 73

If you don't mind making time a factor, you can do the following. Note that I turned your data into a data frame named 'dat'.

dat <- data.frame(Sample=c("A","A", "A", "B","B","B","A","A", "A", "B","B","B","A","A", "A", "B","B","B","A","A", "A", "B","B","B"),
Treatment=c("twiter","twiter","twiter","twiter","twiter","twiter","facebook","facebook","facebook","facebook","facebook","facebook",
            "twiter","twiter","twiter","twiter","twiter","twiter","facebook","facebook","facebook","facebook","facebook","facebook"),
replicate=c(1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3),
time=c( 10,10,10,10,10,10,10,10,10,10,10,10,20,20,20,20,20,20,20,20,20,20,20,20),
points=c(20,40,80,20,60,120, 30,100,55, 28, 45,90, 80,20,100, 40,90,56,20,30,12,3,5,8))

dat %>%
  mutate(time = factor(time)) %>%
  ggplot(aes(x=time, y=points, color=Sample, fill=Sample), alpha=0.1) +
  geom_boxplot(alpha=0.1) +
  geom_point(position = position_dodge(width=0.75), alpha=0.2)+
  theme_bw() 

boxplot by time

Upvotes: 1

Duck
Duck

Reputation: 39613

Try this:

library(dplyr)
library(ggplot2)
#Plot
data %>%
  arrange(Sample) %>%
  mutate(Var=paste(Sample,Treatment),
         Var=factor(Var,levels = unique(Var),ordered = T)) %>%
  ggplot(aes(x=time,
             y=points,
             color=Var, fill=Var,
             group=Var), alpha=0.1) +
  geom_boxplot(alpha=0.1)+
  geom_point(position = position_dodge(width=0.75), alpha=0.2)+
  theme_bw()+
  scale_color_manual(values=c('tomato','tomato','cyan3','cyan3'))+
  scale_fill_manual(values=c('tomato','tomato','cyan3','cyan3'))

Output:

enter image description here

Upvotes: 1

Related Questions