Hi:
Here's a way to do it with ggplot2 and the gridExtra package - I'll
let you handle the theming.
library(ggplot2)
library(gridExtra)
# library(scales)
# library(grid)
p <- ggplot(DF, aes(x = mon, y = Lengths)) +
geom_line() +
scale_x_date(labels = scales::date_format("%b\n%Y")) +
xlab(NULL)
q <- ggplot(DF, aes(x = mon, y = Weights)) +
geom_line() +
scale_x_date(labels = scales::date_format("%b\n%Y")) +
xlab(NULL)
gp <- ggplotGrob(p)
gq <- ggplotGrob(q)
maxWidth = grid::unit.pmax(gp$widths[2:5], gq$widths[2:5])
gp$widths[2:5] <- as.list(maxWidth)
gq$widths[2:5] <- as.list(maxWidth)
grid.arrange(gp, gq, ncol=1)
There's a somewhat more efficient way to do this with the gtable
package, but this works for your example. The code chunk vertically
aligns the two graphics regions, which is necessary because the y-axis
labels have different widths.
Aside: If you want the tick marks to be the same color as the grid
lines in your graph, add axis.ticks to the theme() call. It uses
element_line(); make the color of the ticks the same as those of the
grid lines.
Dennis