假设我有以下数据:
mymatrix1 <- matrix(data = 0, nrow = 105, ncol =2)
mymatrix2 <- matrix(data = 0, nrow = 108, ncol =2)
mymatrix3 <- matrix(data = 0, nrow = 112, ncol =2)
mymatrix1[,1] <- sample(0:1, 105, replace= TRUE)
mymatrix1[,2] <- rnorm(105, 52, 2)
mymatrix2[,1] <- sample(0:1, 108, replace= TRUE)
mymatrix2[,2] <- rnorm(108, 60, 2)
mymatrix3[,1] <- sample(0:1, 112, replace= TRUE)
mymatrix3[,2] <- rnorm(112, 70, 2)
for(i in 1:3){ colnames(mydata[[i]]) <- c("class", "readcount") }
mydata <- list(mymatrix1, mymatrix2,mymatrix3)
我得到mydata [[1]]的情节,因为我想要它使用以下内容:
qplot(class, readcount, data = as.data.frame(mydata[[1]]), geom="boxplot", fill = factor(class))+
geom_jitter(position=position_jitter(w=0.1,h=0.1))+
scale_x_continuous(breaks=c(0,1), labels=c("0", "1"))
现在,我如何将所有箱形图彼此相邻(所以迭代列表)?使用grid.arrange?但这会给我不同的尺度......如果我想要相同的比例呢?有人可以同时出现吗?
答案 0 :(得分:1)
结帐facet_wrap
。
library(ggplot2)
mymatrix1 <- matrix(data = 0, nrow = 105, ncol =2)
mymatrix2 <- matrix(data = 0, nrow = 108, ncol =2)
mymatrix3 <- matrix(data = 0, nrow = 112, ncol =2)
mymatrix1[,1] <- sample(0:1, 105, replace= TRUE)
mymatrix1[,2] <- rnorm(105, 52, 2)
mymatrix2[,1] <- sample(0:1, 108, replace= TRUE)
mymatrix2[,2] <- rnorm(108, 60, 2)
mymatrix3[,1] <- sample(0:1, 112, replace= TRUE)
mymatrix3[,2] <- rnorm(112, 70, 2)
mydata <- list(mymatrix1, mymatrix2,mymatrix3)
for(i in 1:3){
mydata[[i]] <- cbind(mydata[[i]], i)
colnames(mydata[[i]]) <- c("class", "readcount", "group")
}
mydata <- as.data.frame(do.call(rbind, mydata))
## fixed scales
p <- qplot(class, readcount, data = mydata, geom="boxplot", fill = factor(class)) +
geom_jitter(position=position_jitter(w=0.1,h=0.1)) +
scale_x_continuous(breaks=c(0,1), labels=c("0", "1")) +
facet_wrap(~ group)
## free scales
p + facet_wrap(~ group, scales = "free")