如何将一个普通的ggplot与cowplot中的一个多面体对齐? [英] How to align an ordinary ggplot with a faceted one in cowplot?

查看:155
本文介绍了如何将一个普通的ggplot与cowplot中的一个多面体对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过使用 cowplot 软件包以发布就绪的方式安排地块。

我只想让面板成为同样大小和标签。



可复制的expample



  library(ggplot2)
library (cowplot)

gg1 < - ggplot(mtcars)+
geom_point(aes(x = mpg,y = hp))+
theme_bw()+
主题(aspect.ratio = 1)

gg2 < - ggplot(mtcars)+
geom_point(aes(x = mpg,y = hp,fill = cyl))+
facet_wrap(〜cyl,ncol = 2)+
theme_bw()+
主题(aspect.ratio = 1,
legend.position ='none')

输出< - plot_grid(gg1,gg2,labels = c('A','B'),label_size = 20)
print(输出)

代码产生这个图。



正如您所看到的,水平轴线不会与面板的上边缘匹配。

cowplot 的参数 align 不适用于

任何想法?

解决方案

既然这是一个关于cowplot和复杂路线的投票最高的问题,我想指出,现在cowplot有一些功能,用于对齐方块图。 (我是软件包的作者)。但是,它们不适用于这种特殊情况!



例如,这可以工作(使用 plot_grid()):

  gg1 < -  ggplot(mtcars)+ 
geom_point(aes(x = mpg,y = hp))+
theme_bw()

gg2 < - ggplot( mtcars)+
geom_point(aes(x = mpg,y = hp,fill = cyl))+
facet_wrap(〜cyl,ncol = 2)+
theme_bw()+
theme(legend.position ='none')

plot_grid(gg1,gg2,labels = c('A','B'),label_size = 20,
align ='h ',axis ='tb')



我们也可以这样做,以获得不同类型的对齐方式(取决于您是否希望将方面条视为情节的一部分):

  plot_grid(gg1,gg2,labels = c('A','B'),label_size = 20,
align ='h',axis ='b')



现在为什么我说这不适合这种情况?因为,如果您查看问题中的原始代码,您会看到我删除了主题(aspect.ratio = 1)设置。只要不强制特定的高宽比,cowplot就可以对齐地块,因为它用于对齐地块的方法通常会修改各个地块的高宽比。


I'm trying to arrange plots in a publication-ready way with the use of cowplot package.
I just want the panels to be equally sized and labeled.

Reproducible expample

library(ggplot2)
library(cowplot)

gg1 <- ggplot(mtcars)+
        geom_point(aes(x=mpg,y=hp))+
        theme_bw()+
        theme(aspect.ratio=1)

gg2 <- ggplot(mtcars)+
        geom_point(aes(x=mpg,y=hp,fill=cyl))+
        facet_wrap(~cyl,ncol=2)+
        theme_bw()+
        theme(aspect.ratio=1,
              legend.position='none')

output <- plot_grid(gg1,gg2, labels = c('A','B'),label_size = 20)
print(output)

The code produces this plot.

As you may see, neither the horizontal axises match, nor do the upper edges of the panels.

The argument align from cowplot does not work with faceted plots.

Any ideas?

解决方案

Since this is one of the highest voted question regarding cowplot and complex alignments, I wanted to point out that cowplot now does have some functionality for aligning faceted plots. (I'm the package author.) However, they don't work in this particular case!

For example, this works (using the axis option in plot_grid()):

gg1 <- ggplot(mtcars) +
  geom_point(aes(x=mpg, y=hp)) +
  theme_bw()

gg2 <- ggplot(mtcars)+
  geom_point(aes(x=mpg, y=hp, fill=cyl)) +
  facet_wrap(~cyl, ncol=2) +
  theme_bw() +
  theme(legend.position='none')

plot_grid(gg1, gg2, labels = c('A','B'), label_size = 20,
          align = 'h', axis = 'tb')

We can also do this the following, to get a different type of alignment (depending on whether you want the facet strip to be counted as part of the plot or not):

plot_grid(gg1, gg2, labels = c('A', 'B'), label_size = 20,
          align = 'h', axis = 'b')

Now why did I say it doesn't work for this case? Because, if you look at the original code in the question, you'll see that there was a theme(aspect.ratio=1) setting that I removed. cowplot can align plots as long as you don't force a specific aspect ratio, because the method it uses to align plots typically modifies the aspect ratio of the individual plots.

这篇关于如何将一个普通的ggplot与cowplot中的一个多面体对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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