Reputation: 532
I'm trying to control the height of plots in a grid layout in ggplot2. I found some promising examples for page spacing using viewport.
I managed to control the column width. I want the first column to be a third of the page width.
However, I want the second row of figures to sit close to the first row. I tried fooling around with the plot margins, but I'm unable to affect the spacing between the two rows.
Here's the code that draws my figures.
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))
Upvotes: 0
Views: 234
Reputation: 60070
If you're using something like coord_fixed()
then the plots won't automatically expand to fill all available space. Finding a good plot size that will show all the plots without too much whitespace is often a bit of a process of trial and error (although I guess you could do some rough math to figure it out based on the ratio of width to height).
In that case, rather than solving it with code, you can just view the plot in a resizable window (e.g. by clicking "Zoom" in RStudio), and manually resize the window to figure out a good size.
Upvotes: 1