在cowplot中使用axis_canvas的边缘图:如何插入主面板和边缘图之间的差距 [英] Marginal plots using axis_canvas in cowplot: How to insert gap between main panel and marginal plots
问题描述
以下是对
正如我们在这个例子中看到的那样,边际盒形图直接触摸主要的绘图面板。目标是产生一些差距。如何做到这一点?
我看到两个选择:
插入空图
我们可以应用 insert_xaxis_grob()
/ insert_yaxis_grob()
迭代地插入多个grobs,其中一个可以是空的。通过这种方式,我们可以在边缘图的任一侧插入指定数量的空间。在这里,我展示了如何在内部做到这一点,以便在主面板和边缘图之间产生差距:
#pmain,xbox,ybox被定义为问题
pnull< - ggdraw()#generate empty plot
p1< - insert_xaxis_grob(
insert_xaxis_grob(pmain,xbox ,grid :: unit(0.6,in),position =top),
pnull,grid :: unit(0.2,in),position =top)
p2 < - insert_yaxis_grob(
insert_yaxis_grob(p1,ybox,grid :: unit(0.6,in),position =right),
pnull,grid :: unit(0.2, in),position =right)
ggdraw(p2)
在边际地块上创造空白
或者,由于边界图是用ggplot2绘制的,所以我们可以指定轴lim它在适当的位置产生空间。也就是说,在原始代码中不是
xbox
和 ybox
,我们定义 xbox2
和 ybox2
via: xbox2 < - axis_canvas (pmain,axis =x,coord_flip = TRUE)+
geom_boxplot(data = mpg,aes(y = cty,x = as.numeric(factor(cyl)),color = factor(cyl))) +
scale_x_continuous(limits = c(-2,4.5))+ coord_flip()
ybox2 < - axis_canvas(pmain,axis =y)+
geom_boxplot(data = mpg, aes(y = hwy,x = as.numeric(factor(cyl)),color = factor(cyl)))+
scale_x_continuous(limits = c(-2,4.5))
1 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)
待下tand这里发生的事情,我们一起比较 xbox
和 xbox2
:
plot_grid(xbox + panel_border(black),
xbox2 + panel_border(black),nrow = 1,scale = 0.9)
我们看到> xbox2
(右侧)在底部有多余的空间,这是通过以-2开始轴创建的,即使第一个箱形图是位于位置1.有关如何选择这些边界图的轴范围的更多信息,请参见此处。
The following came up in a comment to this post: When making marginal plots with the axis_canvas()
function in cowplot, how can we create a gap between the main plot and the marginal plot?
Example code:
require(cowplot)
pmain <- ggplot(data = mpg, aes(x = cty, y = hwy, color = factor(cyl))) +
geom_point() +
xlab("City driving (miles/gallon)") +
ylab("Highway driving (miles/gallon)") +
theme_minimal()
xbox <- axis_canvas(pmain, axis = "x", coord_flip = TRUE) +
geom_boxplot(data = mpg, aes(y = cty, x = factor(cyl), color = factor(cyl))) +
scale_x_discrete() + coord_flip()
ybox <- axis_canvas(pmain, axis = "y") +
geom_boxplot(data = mpg, aes(y = hwy, x = factor(cyl), color = factor(cyl))) +
scale_x_discrete()
p1 <- insert_xaxis_grob(pmain, xbox, grid::unit(0.6, "in"), position = "top")
p2 <- insert_yaxis_grob(p1, ybox, grid::unit(0.6, "in"), position = "right")
ggdraw(p2)
As we can see in this example, the marginal boxplots directly touch the main plot panel. The goal is to generate some gap. How can this be done?
I see two options:
Insert empty plot
We can apply the insert_xaxis_grob()
/ insert_yaxis_grob()
functions iteratively to insert multiple grobs, one of which can be empty. In this way, we can insert a specified amount of space on either side of the marginal plots. Here I'm showing how to do this on the inside, to generate a gap between the main panel and the marginal plots:
# pmain, xbox, ybox are defined as in the question
pnull <- ggdraw() # generate empty plot
p1 <- insert_xaxis_grob(
insert_xaxis_grob(pmain, xbox, grid::unit(0.6, "in"), position = "top"),
pnull, grid::unit(0.2, "in"), position = "top")
p2 <- insert_yaxis_grob(
insert_yaxis_grob(p1, ybox, grid::unit(0.6, "in"), position = "right"),
pnull, grid::unit(0.2, "in"), position = "right")
ggdraw(p2)
Create gap in the marginal plots
Alternatively, since the marginal plots are drawn with ggplot2, we can just specify axis limits that generate space in the appropriate location. I.e., instead of xbox
and ybox
in the original code, we define xbox2
and ybox2
via:
xbox2 <- axis_canvas(pmain, axis = "x", coord_flip = TRUE) +
geom_boxplot(data = mpg, aes(y = cty, x = as.numeric(factor(cyl)), color = factor(cyl))) +
scale_x_continuous(limits = c(-2, 4.5)) + coord_flip()
ybox2 <- axis_canvas(pmain, axis = "y") +
geom_boxplot(data = mpg, aes(y = hwy, x = as.numeric(factor(cyl)), color = factor(cyl))) +
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)
To understand what is happening here, let's compare xbox
and xbox2
side by side:
plot_grid(xbox + panel_border("black"),
xbox2 + panel_border("black"), nrow = 1, scale = 0.9)
We see how xbox2
(on the right) has extra space at the bottom, which was created by starting the axis at -2, even though the first box plot is located at position 1. More information on how to choose the axis ranges for these marginal plots can be found here.
这篇关于在cowplot中使用axis_canvas的边缘图:如何插入主面板和边缘图之间的差距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!