Cowplot:如何自定义主面板与面板之间的间隙通过修改轴极限来绘制边际图? [英] cowplot: How to customize the gaps between main panel & marginal plots by modifying axis limits?

查看:209
本文介绍了Cowplot:如何自定义主面板与面板之间的间隙通过修改轴极限来绘制边际图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是@ClausWilke(

This is a followup question on one of the solutions provided by @ClausWilke (see post) to insert gap between main panel & marginal plots. How does one decide the (scale_x_continuous) limits? Also, what’ll happen if we used "NA" as the upper limit?

# Example with limits set to: (-2,4.5)
require(ggplot2)
require(cowplot)

pmain <- ggplot(data = mpg, aes(x = cty, y = hwy)) + 
  geom_point() + 
  xlab("City driving (miles/gallon)") +
  ylab("Highway driving (miles/gallon)") + 
  theme_grey()

xbox2 <- axis_canvas(pmain, axis = "x", coord_flip = TRUE) + 
  geom_boxplot(data = mpg, aes(y = cty, x = 1))  + 
  scale_x_continuous(limits = c(-2, 4.5)) + coord_flip()

ybox2 <- axis_canvas(pmain, axis = "y") + 
  geom_boxplot(data = mpg, aes(y = hwy, x = 1)) + 
  scale_x_continuous(limits = c(-2, 4.5))

p1 <- insert_xaxis_grob(pmain, xbox2, grid::unit(0.8, "in"), position = "top")
p2 <- insert_yaxis_grob(p1, ybox2, grid::unit(0.8, "in"), position = "right")

ggdraw(p2)

推荐答案

使用连续的x轴刻度,您可以使用expand_limits在主图和边缘图之间添加一些空白区域.

With a continuous x-axis scale, you can use expand_limits to add some empty space between the main plot and the marginal plots.

例如,您的ybox2是具有连续x轴刻度的单箱图.默认情况下,该图的x范围大约为0.6到1.4(您可以通过运行ggplot(mpg, aes(y=hwy, x=1)) + geom_boxplot()看到它).以下代码将x轴下限设置为0.2,这意味着x轴范围的约0.4/(0.8 + 0.4)= 33%将是主图和边际图之间的边距.我们对xbox2做同样的事情.

For example, your ybox2 is a single box plot with a continuous x-axis scale. By default, the x-range for this plot is roughly 0.6 to 1.4 (you can see this by running ggplot(mpg, aes(y=hwy, x=1)) + geom_boxplot()). The following code sets the lower x-axis limit to 0.2 instead, meaning that about 0.4/(0.8+0.4) = 33% of the x-axis range will be the margin between the main plot and the marginal plot. We do the same for xbox2.

ybox2 <- axis_canvas(pmain, axis = "y") + 
  geom_boxplot(data = mpg, aes(y = hwy, x = 1)) +
  expand_limits(x = 0.2)

xbox2 <- axis_canvas(pmain, axis = "x", coord_flip = TRUE) + 
  geom_boxplot(data = mpg, aes(y = cty, x = 1))  + 
  coord_flip() +
  expand_limits(x = 0.2)

p1 <- insert_xaxis_grob(pmain, xbox2, grid::unit(0.4, "in"), position = "top")
p2 <- insert_yaxis_grob(p1, ybox2, grid::unit(0.4, "in"), position = "right")

ggdraw(p2)

这篇关于Cowplot:如何自定义主面板与面板之间的间隙通过修改轴极限来绘制边际图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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