如何在不同页面上执行 ggplot2 facet_wrap?
How to do ggplot2 facet_wrap on different pages?
我需要在不同的页面上绘制 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, aes(x=variable,y=value,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", "LRP1", "COLEC12", "COLEC12",
"COLEC12", "ENTPD1", "ENTPD1", "ENTPD1", "CMKLR1", "CMKLR1",
"CMKLR1", "CANX", "CANX", "CANX", "CD151", "CD151", "CD151",
"CD276", "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
(没有 ggforce
)
由于您没有post问题中的数据样本,我使用的是钻石数据集。
波纹管有两种选择 'pagified' 刻面环绕,一种不带 ggforce::facet_wrap_paginate
,一种带
编辑 我假设您想绘制您提供的数据样本的长(融化)形式。
数据样本:
summary.table <- structure(list(Genes = c("LRP1", "LRP1", "LRP1", "COLEC12", "COLEC12",
"COLEC12", "ENTPD1", "ENTPD1", "ENTPD1", "CMKLR1", "CMKLR1",
"CMKLR1", "CANX", "CANX", "CANX", "CD151", "CD151", "CD151",
"CD276", "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
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'))
}
与facet_wrap_paginate
这是在 facet_wrap_paginate 的文档中 post 编写的相同示例,但使用 for 循环和 ggsave 保存所有页面:
library(ggplot2)
library(ggforce)
summary.table_melted <- reshape2::melt(summary.table, id.vars='Genes')
p <- ggplot(summary.table_melted) +
geom_violin(aes(variable, value))+
theme(axis.text.x = element_text(angle = 90))+
facet_wrap_paginate(~ Genes, ncol = 2, nrow = 2)
for(i in 1:n_pages(p)){
p_save <- p +
facet_wrap_paginate(~ Genes, ncol = 2, nrow = 2, page = i)
ggsave(plot = p_save, filename = paste0('Downloads/page_', i, '.jpg'))
}
我需要在不同的页面上绘制 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, aes(x=variable,y=value,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", "LRP1", "COLEC12", "COLEC12",
"COLEC12", "ENTPD1", "ENTPD1", "ENTPD1", "CMKLR1", "CMKLR1",
"CMKLR1", "CANX", "CANX", "CANX", "CD151", "CD151", "CD151",
"CD276", "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
(没有 ggforce
)
由于您没有post问题中的数据样本,我使用的是钻石数据集。
波纹管有两种选择 'pagified' 刻面环绕,一种不带 ggforce::facet_wrap_paginate
,一种带
编辑 我假设您想绘制您提供的数据样本的长(融化)形式。
数据样本:
summary.table <- structure(list(Genes = c("LRP1", "LRP1", "LRP1", "COLEC12", "COLEC12",
"COLEC12", "ENTPD1", "ENTPD1", "ENTPD1", "CMKLR1", "CMKLR1",
"CMKLR1", "CANX", "CANX", "CANX", "CD151", "CD151", "CD151",
"CD276", "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
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'))
}
与facet_wrap_paginate
这是在 facet_wrap_paginate 的文档中 post 编写的相同示例,但使用 for 循环和 ggsave 保存所有页面:
library(ggplot2)
library(ggforce)
summary.table_melted <- reshape2::melt(summary.table, id.vars='Genes')
p <- ggplot(summary.table_melted) +
geom_violin(aes(variable, value))+
theme(axis.text.x = element_text(angle = 90))+
facet_wrap_paginate(~ Genes, ncol = 2, nrow = 2)
for(i in 1:n_pages(p)){
p_save <- p +
facet_wrap_paginate(~ Genes, ncol = 2, nrow = 2, page = i)
ggsave(plot = p_save, filename = paste0('Downloads/page_', i, '.jpg'))
}