2
votes

I have five files with data in matrix form and I am plotting it using geom_boxplot. each boxplot corresponds to a file.

What I want to achieve is for only certain files say here for div1,div3,div5 I want to plot boxplot with data points overlaid on the boxplot. I could add data points using geom_jitter but i had to separate those plots with data points from the only boxplots plots.

Since I want to preserve the order of plotting the files..i.e div0,div1.. etc. I could not plot data points for only certain boxplots.

How can add overlay data points for only certain boxplots and not all?

files <- c(div0,div1,div2,div3,div4,div5)

p1 <- ggplot(moltenNew,aes(x=L1,y=value,colour=L1))+ ylim(0.3,0.8) +
       geom_boxplot() + facet_wrap(~variable,nrow=1) + scale_x_discrete(limits = basename(files) ,labels = basename(files))


 ![enter image description here][1] 
1

1 Answers

2
votes

You could use subset:

set.seed(1)
moltenNew <- rbind(
  data.frame(value = rnorm(20, 50, 20), L1 = gl(2, 10), variable = 1),
  data.frame(value = rnorm(20, 100, 100), L1 = gl(2, 10), variable = 2),
  data.frame(value = rnorm(20, 75, 10), L1 = gl(2, 10), variable = 3) 
)
moltenNew
library(ggplot2)
ggplot(moltenNew,aes(x=L1,y=value,colour=L1)) + 
  geom_boxplot() + 
  facet_wrap(~variable,nrow=1, scale = "free_y") + 
  geom_point(subset = .(variable == 2), position = position_jitter(width = .2))

enter image description here