2
votes

I've three replicates of my experimental sample(F) and three replicates of control samples(C). Each experimental sample has 100 data points whereas each control has 70 data points. For the experimental data points, there are sub-categories like 4E,5E,7E,8E and for control there is only a single category CE.

Here's the code to generate some emulated data:

library(ggplot2)
set.seed(12345)
evals <- c( rep("4E",20), rep("5E",20), rep("7E",40), rep("8E",20))
subE <- c(sample(evals),sample(evals),sample(evals),rep("CE",70),rep("CE",70),rep("CE",70))
pwvr <- c(rnorm(100),rnorm(100),rnorm(100),rnorm(70,1.0),rnorm(70,1.1),rnorm(70,1.2))
expT <- c(rep("F",100*3),rep("C",70*3))
repX <- c(rep(1,100),rep(2,100),rep(3,100),rep(1,70),rep(2,70),rep(3,70))
myData.df <- data.frame(subE=as.factor(subE), pwvr = pwvr, expT= as.factor(expT), repX= as.factor(repX))
dim(myData.df)

What I want to do, is to draw Box and Jitter plots for the factor levels 4E,5E,7E,8E along with a boxplot of the combined values of these four levels. I don't know how to do that. Do I need to create another level clubbing all the corresponding values?

Secondly can anybody tell how to reorganize the appearance of orders in along the X-axis, like how to plot the following order 8E,7E,5E,4E etc.

The following code generates the plot for individual levels, but I need to have the box/jitter for the combined levels as well.

myGreen <- "forestgreen"
myBlue <- "dodgerblue2"
allboxCol <- c(rep(myGreen,1),rep(myBlue,1))

pw.boxplot <- ggplot(myData.df, aes(x=subE,y=pwvr, fill= expT)) +
                geom_jitter(position=position_jitter(width=.2, height=0),alpha=0.15, aes(col= expT)) + scale_color_manual(values=allboxCol) +
                geom_boxplot(outlier.shape = NA, fatten = 0.01, lwd=1.0,alpha=0.5,width=0.6) + 
                theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
                panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) 
pw.boxplot + scale_fill_manual(values=allboxCol) + facet_grid( .~ expT + repX , scales="free", space = "free" )   

Boxplot of Factor Levels

2

2 Answers

1
votes

It is indeed an easy solution to just repeat the data but assigning a new level, I called it all. You can explicitly specify the order of the levels:

library(ggplot2)
library(dplyr)
set.seed(12345)
evals <- c( rep("4E",20), rep("5E",20), rep("7E",40), rep("8E",20))
subE <- c(sample(evals),sample(evals),sample(evals),rep("CE",70),rep("CE",70),rep("CE",70))
pwvr <- c(rnorm(100),rnorm(100),rnorm(100),rnorm(70,1.0),rnorm(70,1.1),rnorm(70,1.2))
expT <- c(rep("F",100*3),rep("C",70*3))
repX <- c(rep(1,100),rep(2,100),rep(3,100),rep(1,70),rep(2,70),rep(3,70))
myData.df <- data.frame(subE=subE, pwvr = pwvr, expT= expT, repX= repX,
                        stringsAsFactors = FALSE)

add_data <- myData.df %>% 
  filter(subE != "CE") %>% 
  mutate(subE = "all")

myData.df <- bind_rows(myData.df, add_data)
myData.df <- myData.df %>% 
  mutate(subE = as.factor(subE),
         subE = factor(subE, levels = levels(subE)[c(4, 3, 2, 1, 5, 6)]))


myGreen <- "forestgreen"
myBlue <- "dodgerblue2"
allboxCol <- c(rep(myGreen,1),rep(myBlue,1))

pw.boxplot <- ggplot(myData.df, aes(x=subE,y=pwvr, fill= expT)) +
  geom_jitter(position=position_jitter(width=.2, height=0),alpha=0.15, aes(col= expT)) + scale_color_manual(values=allboxCol) +
  geom_boxplot(outlier.shape = NA, fatten = 0.01, lwd=1.0,alpha=0.5,width=0.6) + 
  theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
                     panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) 
pw.boxplot + scale_fill_manual(values=allboxCol) + facet_grid( .~ expT + repX , scales="free", space = "free" )   

enter image description here

4
votes

do you mean?

myGreen <- "forestgreen"
myBlue <- "dodgerblue2"
allboxCol <- c(rep(myGreen,1),rep(myBlue,1))
allboxCol <- c(alpha(allboxCol, 0.2),allboxCol[2])

library(tidyverse)
myData.df %>% 
  as_tibble() %>% 
  mutate(subE = paste0("total_", expT, repX)) %>% 
  filter(expT  != "C") %>% 
  bind_rows(myData.df) %>% 
  mutate(fill = ifelse(grepl("total", subE), paste0("total_",expT), expT)) %>% 
  ggplot(aes(x=subE,y=pwvr, fill= fill)) +
     geom_jitter(position=position_jitter(width=.2, height=0),alpha=0.15, aes(col= fill), show.legend = F) + 
     geom_boxplot(outlier.shape = NA, fatten = 0.01, lwd=1.0,width=0.6, show.legend = F) + 
     scale_fill_manual(values=allboxCol) +
     scale_color_manual(values=allboxCol) +
     theme_bw() + 
     theme(panel.border = element_blank(), panel.grid.major = element_blank(),
                     panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
  facet_grid( .~ expT + repX , scales="free", space = "free" ) 

enter image description here