如何将多个ggplot2对象以相同的比例组合起来 [英] How to combine multiple ggplot2 objects on the same scale

查看:196
本文介绍了如何将多个ggplot2对象以相同的比例组合起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下数据:

pre $ lt; code> mymatrix1 < - matrix(data = 0,nrow = 105,ncol = 2)
mymatrix2 < - 矩阵(数据= 0,nrow = 108,ncol = 2)
mymatrix3 < - 矩阵(数据= 0,nrow = 112,ncol = 2)

mymatrix1 [,1]< - sample(0:1,105,replace = TRUE)
mymatrix1 [,2]< - rorm(105,52,2)
mymatrix2 [ ,1] < - 样本(0:1,108,替换= TRUE)
mymatrix2 [,2] < - rnorm(108,60,2)
mymatrix3 [,1]<示例(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)

$ b我得到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(中断= c(0,1),labels = c(0,1))

现在,我将如何获得彼此相邻的所有箱形图(如此遍历列表)?使用grid.arrange?但是,这会给我不同的规模......如果我想要相同的规模呢?有人可以同时显示吗?

解决方案

查看 facet_wrap

  library(ggplot2)

mymatrix1 < - matrix(data = 0,nrow = 105,ncol = 2 )
mymatrix2 < - 矩阵(数据= 0,nrow = 108,ncol = 2)
mymatrix3 < - 矩阵(数据= 0,nrow = 112,ncol = 2)
$ b $ 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 )

(i在1:3){
mydata [[i]] < - cbind(mydata [[i]],i)
colnames(mydata [ [i]])< - c(class,readcount,group)
}


mydata< - as.data.frame(do .call(rbind,mydata))

##固定比例
p < - qplot(class,readcount,data = mydat一个geom =boxplot,fill = factor(class))+
geom_jitter(position = position_jitter(w = 0.1,h = 0.1))+
scale_x_continuous(breaks = c(0,1) ,标签= c(0,1))+
facet_wrap(〜group)


## free scales
p + facet_wrap(〜group, scale =free)



Suppose I have the following data:

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)

I get the plot for mydata[[1]] as I want it using the following:

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

Now, how would I get all the box plots next to each other (so iterate over the list)? Using grid.arrange? But that would give me different scales... what if I wanted the same scale? Can someone show both?

解决方案

Check out 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")

这篇关于如何将多个ggplot2对象以相同的比例组合起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆