[英]Reproducing ggplot2's facet_wrap plot with alternative functions
I have a data.frame
that I'd like to produce bar plots for with ggplot2
's geom_bar
, faceted by one of the columns in the data.frame
- cluster
:我有一个
data.frame
,我想用ggplot2
的geom_bar
生成条形图,由data.frame
- cluster
中的一列组成:
library(dplyr)
df <- data.frame(cluster = c("c1","c1","c1","c2","c2","c2","c3","c3","c3"), group = rep(c("A","B","C"),3), p = c(0.4,0.2,0.4,0.5,0.5,0.5,0.2,0.3,0.5))
It's clear how to produce this with ggplot2
's facet_wrap
:很清楚如何使用
ggplot2
的facet_wrap
产生这个:
library(ggplot2)
ggplot(df, aes(x = group, y = p, group = cluster, fill = group)) +
geom_bar(stat = 'identity') +
scale_x_discrete(name = NULL,labels = levels(df$group), breaks = sort(unique(df$group))) +
facet_wrap(as.formula("~ cluster")) + theme_minimal() + theme(legend.title = element_blank()) + ylab("Fraction of cells")
But for reasons that are not mentioned in this post (not very relevant) facet_wrap
doesn't work for me and I need to resort to producing a list of plots, one for each cluster, and arrange them myself in a grid:但是由于这篇文章中没有提到的原因(不是很相关)
facet_wrap
对我不起作用,我需要求助于生成一个图列表,每个集群一个,并自己将它们排列在一个网格中:
plot.list <- lapply(unique(df$cluster),function(l){
ggplot(dplyr::filter(df,cluster == l), aes(x = group, y = p, fill = group)) +
geom_bar(stat = 'identity') +
scale_x_discrete(name = NULL,labels = levels(dplyr::filter(df,cluster == l)$group), breaks = sort(unique(dplyr::filter(df,cluster == l)$group))) +
theme_minimal() + theme(legend.title = element_blank(), plot.title = element_text(hjust = 0.5)) + ylab("Fraction of cells") + ggtitle(l)
})
There are two benefits of facet_wrap
that I'm missing by not using it that I'm trying to find out how to retain: facet_wrap
有两个好处,我没有使用它,我试图找出如何保留它:
facet_wrap
does that and perhaps if that information can be extracted from running the plotting that's using facet_wrap
command above?facet_wrap
是如何做到这一点的,也许可以从运行上面使用facet_wrap
命令的绘图中提取该信息?facet_wrap
also automatically shares the axis titles and ticks, but gridExtra::grid.arrange
or scater::multiplot
do not. facet_wrap
也自动共享轴标题和刻度,但gridExtra::grid.arrange
或scater::multiplot
multiplot 不共享。 The only way to achieve that with these functions is to suppress these themes
for all the clusters in the lapply
call which the user does not want to come out with these labels (imitating facet_wrap
).lapply
调用中的所有集群抑制这些themes
,用户不想使用这些标签(模仿facet_wrap
)。 I guess that an answer for question 1 offers an answer to this question (conditioning axis label suppression on the location of the cluster in the desired grid), but if anyone has a more direct solution that'll be great. In other and simple words, I'm looking for a generic way to reproduce the plot grid using facet_wrap
above with gridExtra::grid.arrange
or scater::multiplot
(or any other function) applied to plot.list
换句话说,我正在寻找一种通用方法来重现 plot 网格,使用上面的
facet_wrap
gridExtra::grid.arrange
或scater::multiplot
multiplot (或任何其他函数)应用于plot.list
The dimensions of the facet_wrap plot can be found in the wrap_dims function. facet_wrap plot 的尺寸可以在 wrap_dims function 中找到。
x=ggplot(df, aes(x = group, y = p, group = cluster, fill = group)) +
geom_bar(stat = 'identity') +
scale_x_discrete(name = NULL,labels = levels(df$group), breaks = sort(unique(df$group))) +
facet_wrap(as.formula("~ cluster")) + theme_minimal() + theme(legend.title = element_blank()) + ylab("Fraction of cells")
col=wrap_dims(length(x))[2]
plot.list=list()
for (i in 1:length(unique(df$cluster))) {
y=ggplot(dplyr::filter(df,cluster == unique(df$cluster)[i]), aes(x = group, y = p, fill = group)) +
geom_bar(stat = 'identity') +
scale_x_discrete(name = NULL,labels = levels(dplyr::filter(df,cluster == unique(df$cluster)[i])$group), breaks = sort(unique(dplyr::filter(df,cluster == unique(df$cluster)[i])$group))) +
theme_minimal() + theme(plot.title = element_text(hjust = 0.5)) + ggtitle(unique(df$cluster)) + ylim(0, max(df$p))
if (i<length(unique(df$cluster))) {
y=y+theme(legend.position="none")
}
if (i>1) {
y=y+ylab("")
} else {
y=y+ylab("Fraction of cells")
}
plot.list[[i]]=y
}
do.call(grid.arrange, c(plot.list, ncol=col))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.