使用多点设置图之间的边距 [英] Set margins between plots using multiplot
问题描述
要显示多个图,我使用多图(
上述布局存在两个问题:(1)y坐标不合理,(2)下图的高度面积是小于上方小区的地块面积。下面的代码解决了这些问题:
#左对齐图
#来源:http://stackoverflow.com/ a / 13295880/496488
gA < - ggplotGrob(p1)
gB < - ggplotGrob(p2)
maxWidth = grid :: unit.pmax(gA $ widths [ 2:5],gB $ widths [2:5])
gA $ widths [2:5]< - as.list(maxWidth)
gB $ widths [2:5]< - as.list(maxWidth)
#排列合理的图。使用高度参数来均衡每个绘图区域的高度
grid.arrange(gA,gB,heights = c(0.47,0.53),ncol = 1)
你可以精确地平衡每个小区的高度使用与我们用来左对齐图的相同技巧(而不是通过使用 heights
参数来执行 grid.arrange
),但是之后图表边距会重新加回。我不确定如何处理这个问题,但以下是供参考的代码:
maxHeight = grid :: unit。 pmax(gA $ heights [2:5],gB $ heights [2:5])
gA $ heights [2:5]< - as.list(maxHeight)
gB $ heights [2 :5]< - as.list(maxHeight)
To display multiple plots I use multiplot (http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/), now I have two plots who share the same x-axis range and are plotted above each other:
multiplot(plot1, plot2)
I removed the x-axis labels and title using:
xlab(NULL) + theme(axis.text.x=element_blank(),axis.ticks.x=element_blank())
But there is still a white margin between the two plots. How can I make this margin smaller or remove it?
To reduce space between the plots, remove the bottom margin of the top plot and remove the top margin of the bottom plot. The code below sets these margins to 0, which still results in a tiny bit of white space between the plots. You can make these margins slightly negative (maybe -0.1 or so) to completely remove the white space. Rather than the multiplot
function, we use grid.arrange
from the gridExtra
package to lay out the plots. :
library(grid)
library(gridExtra)
## Create two sample plots with the same x axis using built-in mtcars data frame
# Top plot: Remove bottom margin, x-labels, and x title
p1 = ggplot(mtcars, aes(wt, mpg)) + geom_point() +
xlab(NULL) +
theme(axis.text.x=element_blank(),axis.ticks.x=element_blank(),
plot.margin=unit(c(1,1,0,1), "lines"))
# Bottom plot: Remove top margin
p2 = ggplot(mtcars, aes(wt, carb)) + geom_point() +
theme(plot.margin=unit(c(0,1,1,1), "lines"))
# Lay out plots in one column
grid.arrange(p1, p2, ncol=1)
Two problems with the above layout: (1) the y axes are not justified properly, and (2) the height of the lower plot's plot area is less than that of upper plot's plot area. The code below addresses these issues:
# Left justify plots
# Source: http://stackoverflow.com/a/13295880/496488
gA <- ggplotGrob(p1)
gB <- ggplotGrob(p2)
maxWidth = grid::unit.pmax(gA$widths[2:5], gB$widths[2:5])
gA$widths[2:5] <- as.list(maxWidth)
gB$widths[2:5] <- as.list(maxWidth)
# Lay out justified plots. Use heights argument to equalize heights of each plot area
grid.arrange(gA, gB, heights=c(0.47,0.53), ncol=1)
You can exactly equalize the heights of each plot area using the same trick as we used to left-justify the plots (rather than doing it by eye using the heights
argument to grid.arrange
), but then the plot margins get added back. I'm not sure of how to deal with that, but here's the code for reference:
maxHeight = grid::unit.pmax(gA$heights[2:5], gB$heights[2:5])
gA$heights[2:5] <- as.list(maxHeight)
gB$heights[2:5] <- as.list(maxHeight)
这篇关于使用多点设置图之间的边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!