问题描述
我需要在不同的页面上绘制25张小提琴图。
ggplot(sum.tab_tall,aes(x=variable,y=value,fill=Genes))+
geom_violin()+
theme(axis.text.x = element_text(angle = 90))+
facet_wrap(~Genes,scales = 'fixed',nrow=3)
使用此代码,我在同一页面上获得了25个图,它们是无法读取的。如何将它们分成不同的页面?
我已经尝试过face_wrap_paginate
,但没有成功。
ggplot(data=df,fill=Genes)) +
geom_violin(position=position_dodge2(preserve = 'single')+
facet_wrap_paginate(~c("LRP1","SCRG1","ENTPD1"),ncol = 3,nrow = 3,scales = "fixed",strip.position = "top",page = 3))
> dput(summary.table)
structure(list(Genes = c("LRP1","LRP1","COLEC12","ENTPD1","CMKLR1","CANX","CD151","CD276","CD276"),CAF.unstimulated.6h = c(12.1936,12.0941,12.0306,11.1081,10.9277,11.23,5.62037,5.61798,5.49475,10.218,10.0528,10.1973,12.6661,12.5592,12.5659,10.9451,10.853,10.9677,8.85865,8.79687,8.83243),CAF.unstimulated.48h = c(12.1592,12.2532,12.2616,9.03901,9.47515,9.26094,5.25498,5.39444,5.24641,9.55221,9.62914,9.44706,12.6049,12.6259,12.625,10.8893,10.9459,10.9017,8.86994,8.68394,8.86783),CAF.PDGFCC.stimulated.6h = c(12.0032,12.0774,12.0971,10.8014,10.9064,11.046,5.77506,5.51088,5.4302,10.5959,10.5748,10.5775,12.6446,12.611,12.675,10.8996,10.8724,10.9116,9.21853,9.1611,9.04555),CAF.PDGFCC.stimulated.48h = c(12.3216,12.2989,12.212,9.62478,9.76262,9.62223,5.46608,5.29513,5.26186,10.0617,10.0577,10.0604,12.6986,12.7245,12.7018,10.7661,10.7381,10.8173,9.04076,9.02821,9.20451)),class = "data.frame",row.names = c(NA,-21L))
解决方法
我不确定为什么facet_wrap_paginate
对您不起作用,软件包的安装可能存在问题,但是您只能使用ggplot2
来完成(没有{{1 }})
由于您没有在问题中发布数据样本,因此我使用的是钻石数据集。
以下是两个具有'pagified'刻面包装的选项,一个不带有ggforce
,另一个带有。
编辑,我假设您想绘制提供的数据样本的长(熔融)形式。
数据样本:
ggforce::facet_wrap_paginate
没有facet_wrap_paginate
summary.table <- structure(list(Genes = c("LRP1","LRP1","COLEC12","ENTPD1","CMKLR1","CANX","CD151","CD276","CD276"),CAF.unstimulated.6h = c(12.1936,12.0941,12.0306,11.1081,10.9277,11.23,5.62037,5.61798,5.49475,10.218,10.0528,10.1973,12.6661,12.5592,12.5659,10.9451,10.853,10.9677,8.85865,8.79687,8.83243),CAF.unstimulated.48h = c(12.1592,12.2532,12.2616,9.03901,9.47515,9.26094,5.25498,5.39444,5.24641,9.55221,9.62914,9.44706,12.6049,12.6259,12.625,10.8893,10.9459,10.9017,8.86994,8.68394,8.86783),CAF.PDGFCC.stimulated.6h = c(12.0032,12.0774,12.0971,10.8014,10.9064,11.046,5.77506,5.51088,5.4302,10.5959,10.5748,10.5775,12.6446,12.611,12.675,10.8996,10.8724,10.9116,9.21853,9.1611,9.04555),CAF.PDGFCC.stimulated.48h = c(12.3216,12.2989,12.212,9.62478,9.76262,9.62223,5.46608,5.29513,5.26186,10.0617,10.0577,10.0604,12.6986,12.7245,12.7018,10.7661,10.7381,10.8173,9.04076,9.02821,9.20451)),class = "data.frame",row.names = c(NA,-21L))
使用facet_wrap_paginate
这与facet_wrap_paginate文档中发布的示例相同,但是具有for循环和ggsave来保存所有页面:
library(ggplot2)
library(dplyr)
# save all groups you want to facet by
all_groups <- unique(summary.table_melted$Genes)
n_all_groups <- length(all_groups)
n_col <- 2
n_row <- 2
# split the groups so that you'd have n_col*nrow groups in each split
start_idx <- seq(1,n_all_groups,n_col*n_row)
group_splits <- lapply(start_idx,function(i){
all_groups[i:(i+ n_col*n_row -1)]
})
# now for each group split filter the data and create a plot
i <- 0
for(groups in group_splits){
i <- i + 1
p <- summary.table_melted %>%
filter(Genes %in% groups) %>%
ggplot() +
geom_violin(aes(variable,value))+
theme(axis.text.x = element_text(angle = 90))+
facet_wrap(~ Genes,ncol = 3,nrow = 3)
ggsave(plot = p,filename = paste0('Downloads/page_',i,'.jpg'))
}