r - 如何消除 ggplot2 网格布局中行之间的空格?
问题描述
我正在尝试控制 ggplot2 中网格布局中绘图的高度。我发现了一些使用视口的页面间距有希望的例子。
我设法控制了列宽。我希望第一列是页面宽度的三分之一。
但是,我希望第二排数字靠近第一排。我试着玩弄绘图边距,但我无法影响两行之间的间距。
这是绘制我的数字的代码。
library(ggplot2)
library(gridExtra)
# Generate a vector of times.
t=seq(0, 2 , 0.0001)
# Draw some figures using segments.
df1 <- data.frame(x1 = 0, x2 = 1, y1 = 0, y2 = .1)
open_pipe_p <- ggplot(data = df1) +
theme(panel.background = element_rect(fill = "white"),
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
plot.margin = unit(c(0,0.0,0,0), units="npc")) +
coord_fixed() +
geom_segment(aes(x = x1, y = y1, xend = x2, yend = y1), size = .75) +
geom_segment(aes(x = x1, y = y2, xend = x2, yend = y2), size = .75)
closed_pipe_p <- ggplot(data = df1) +
theme(panel.background = element_rect(fill = "white"),
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
plot.margin = unit(c(0,0.0,0,0), units="npc")) +
coord_fixed() +
geom_segment(aes(x = x1, y = y1, xend = x2, yend = y1), size = .75) +
geom_segment(aes(x = x1, y = y2, xend = x2, yend = y2), size = .75) +
geom_segment(aes(x = x1, y = y1, xend = x1, yend = y2), size = .75) +
xlim(0, 2)
# Draw some sinusoids.
# Parameters of sinusoid.
A <- 1
f <- .5
phi <- pi / 2
# Y values.
y <- A * sin(2 * pi * f * t + phi)
df_sin <- data.frame(cbind(t, y))
# I only need 1 second.
df_sin <- df_sin[df_sin$t <= 1, ]
df_sin$y[df_sin$t > 1] <- NA
open_wave_p <- ggplot(data = df_sin) +
theme(panel.background = element_rect(fill = "white"),
axis.line = element_line(),
axis.text.y = element_blank(),
axis.title = element_blank(),
axis.ticks.y = element_blank(),
plot.margin = unit(c(0,0.0,0,0), units="npc")) +
scale_x_continuous(breaks = seq(0, 1, .2),
expand = c(0, 0)) +
coord_fixed(ratio = .1) +
geom_line(mapping = aes(x = t, y = y)) +
geom_line(mapping = aes(x = t, y = -y))
A <- 1
f <- .25
phi <- 0
y <- A * sin(2 * pi * f * t + phi)
df_sin <- data.frame(cbind(t, y))
closed_wave_p <- ggplot(data = df_sin) +
theme(panel.background = element_rect(fill = "white"),
axis.line = element_line(),
axis.text.y = element_blank(),
axis.title = element_blank(),
axis.ticks.y = element_blank(),
plot.margin = unit(c(0,0.0,0,0), units="npc")) +
scale_x_continuous(breaks = seq(0, 1, .2),
expand = c(0, 0)) +
coord_fixed(ratio = .1) +
geom_line(mapping = aes(x = t, y = y)) +
geom_line(mapping = aes(x = t, y = -y))
# Set up the grid.
grid.newpage()
pushViewport(viewport(layout=grid.layout(
nrow = 2,
ncol = 2,
widths = c(0.333, 0.667),
heights = c(0.25, 0.75))))
print(open_pipe_p, vp=viewport(layout.pos.row=1,layout.pos.col=1))
print(closed_pipe_p, vp=viewport(layout.pos.row=1,layout.pos.col=2))
print(open_wave_p, vp=viewport(layout.pos.row=2,layout.pos.col=1))
print(closed_wave_p, vp=viewport(layout.pos.row=2,layout.pos.col=2))
解决方案
如果您使用类似的东西,coord_fixed()
那么这些图将不会自动扩展以填充所有可用空间。找到一个合适的绘图大小来显示所有绘图而没有太多空白通常是一个反复试验的过程(尽管我猜你可以做一些粗略的数学来根据宽度与高度的比率来计算它)。
在这种情况下,您可以在可调整大小的窗口中查看绘图(例如,通过单击 RStudio 中的“缩放”),而不是用代码解决它,然后手动调整窗口大小以找出合适的大小。
推荐阅读
- css - 带下拉菜单的等宽导航栏链接
- plotly - 设置plotly.js 3D绘图高度和宽度的大小
- python - Python - 从字符串制作正确的列表
- ios - 重新签名的 AppStore .ipa 到 Enterprise In-House 崩溃
- python - Python - 从复制的 URL 中检索文本
- arrays - Google Sheet SUMIF 数组公式不起作用
- vb.net - 触发自动完成下拉菜单以在点击时显示
- wordpress - MAMP 可以被黑到什么程度?
- python - 在 Python whl 文件中模拟函数
- swift - 使用多个关键字进行 Google 附近搜索