在两个图之间为geom_tile设置特定的图块大小 [英] Setting specific tile size for geom_tile between two graphs

查看:65
本文介绍了在两个图之间为geom_tile设置特定的图块大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里可能会遗漏一些简单的东西,但是我的目标是从两个单独的矩阵绘制两个热图.矩阵的尺寸不同,但是我希望每个单独绘制的图块在两个图形之间都具有相同的物理度量.然后,图形的大小应取决于矩阵的尺寸,因为图形之间的瓦片大小将是恒定的.

Might be missing something simple here, but my goal is to plot two heatmaps from two separate matrices. The matrices are of different dimensions, but I want each of the individual plotted tiles to be the same physical measurements between the two graphs. The graph size should then be dependent on the matrix dimensions, since the tile size will be constant between the graphs.

这是两个不同大小的矩阵以及我要制作的图形的精简版本的示例.

Here is an example of two different size matrices and a stripped down version of the graphs I want to make.

mat_lg <- matrix(rexp(20, rate=.1), ncol = 5)

melt(mat_lg) %>%
  ggplot(aes(x = Var1, y = Var2, fill = value)) +
  geom_tile() +
  theme_bw() +
  coord_equal()

mat_sm <- matrix(rexp(12, rate = .1), ncol = 3)

melt(mat_sm) %>%
  ggplot(aes(x = Var1, y = Var2, fill = value)) +
  geom_tile() +
  theme_bw() +
  coord_equal()

由mat_lg制成的图形具有较小的图块...

The graph made from mat_lg has smaller tiles...

与使用mat_sm制作的图形中的图块相比...

compared to the tiles in the graph made from mat_sm...

我已经使用了geom_tile中的width/height设置,但是无法实现我的目标.还玩过grid.arrange以及facet_wrap设置,但无济于事.

I've played around with the width/height settings in geom_tile, but was unable to achieve my goal. Have also played with grid.arrange as well as facet_wrap settings but to no avail.

我的猜测是,为了获得具有相同物理尺寸的图块,我需要根据初始矩阵尺寸更改最终图块大小.非常感谢您对此事有任何帮助!

My guess is that I need to change the final plot size based on my initial matrix dimension in order to get the tiles with the same physical measurements. Would gladly appreciate any help on this matter!

推荐答案

如果两个矩阵至少具有一个相等的维,则可以直接通过 egg :: ggarrange()完成,将情节面板排成一行:

If the two matrices have at least one dimension that is equal, then this is accomplished directly by egg::ggarrange(), which lines up the plot panels:

## Save plots into variables
gg1 <- melt(mat_lg) %>% ggplot(...)
gg2 <- melt(mat_sm) %>% ggplot(...)

## Let ggarrange line up the panels
egg::ggarrange( gg1, gg2 )

如果两个矩阵的尺寸完全不同,请使用 egg :: set_panel_size 显式调整面板尺寸:

If dimensions of the two matrices differ entirely, use egg::set_panel_size to adjust the panel sizes explicitly:

p1 <- egg::set_panel_size( gg1, height=unit(ncol(mat_lg), "cm"),
                          width=unit(nrow(mat_lg), "cm") )
p2 <- egg::set_panel_size( gg2, height=unit(ncol(mat_sm), "cm"),
                          width=unit(nrow(mat_sm), "cm") )
cowplot::plot_grid( p1, p2, nrow=1 )

这篇关于在两个图之间为geom_tile设置特定的图块大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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