Back in 2021 using gridArrange I was able to arrange two plots vertically so the values of the x-axis align perfectly. Here is the plot I produced then:
Notice how the x-axis values align in the top and bottom plot. Now I'm trying to produce a similar plot using new data. Here is the plot I produced:
In the present plot the plot areas are not aligned and so the x-axis values do not align vertically.
The code that produced these plots is virtually identical. Here is the 2021 code:
the.max <- ceiling(max(c(tab.13.3$diff, peeps$measure))) +1
the.min <- floor(min(c(tab.13.3$diff, peeps$measure))) -1
items.pts <- ggplot(tab.13.3, aes(x=diff, y=Item)) +
geom_point() +
scale_x_continuous(limits=c(the.min, the.max))
peeps.hist <- ggplot(peeps, aes(x=measure)) +
geom_histogram() +
scale_x_continuous(limits=c(the.min, the.max))
ggpeeps <- ggplotGrob(peeps.hist)
ggitems <- ggplotGrob(items.pts)
maxWidth = grid::unit.pmax(ggpeeps$widths[2:5], ggitems$widths[2:5])
ggpeeps$widths[2:5] <- as.list(maxWidth)
ggitems$widths[2:5] <- as.list(maxWidth)
grid.arrange(ggpeeps, ggitems, ncol=1,
heights=c(3, 2),
top=paste("Persons and Items for", params$themeas))
The data sets look like this: peeps:
entry measure NAME person.id network_id net.role
<dbl> <dbl> <chr> <dbl> <dbl> <dbl>
1 1 4.86 4265 1 4 4265 1 4
2 2 -0.25 6353 1 4 6353 1 4
3 3 4.86 4273 1 4 4273 1 4
4 4 3.49 4256 1 4 4256 1 4
5 5 3.49 2545 1 3 2545 1 3
6 6 4.31 6352 1 4 6352 1 4
Items:
Entry Item name value
<dbl> <fct> <chr> <dbl>
1 6 so_part_value_8 cat2 -0.95
2 6 so_part_value_8 cat3 0.37
3 6 so_part_value_8 cat4 1.76
4 6 so_part_value_8 cat5 4.38
5 1 so_part_ben_1 cat2 -3.89
6 1 so_part_ben_1 cat3 -1.42
The code I'm using now that results in misaligned axis values is:
the.max <- ceiling(max(c(tab.13.3$value, peeps$measure))) +1
the.min <- floor(min(c(tab.13.3$value, peeps$measure))) -1
items.pts <- ggplot(tab.13.3, aes(x=value, y=Item)) +
geom_point() +
scale_x_continuous(limits=c(the.min, the.max))
peeps.hist <- ggplot(peeps, aes(x=measure)) +
geom_histogram() +
scale_x_continuous(limits=c(the.min, the.max))
ggpeeps <- ggplotGrob(peeps.hist)
ggitems <- ggplotGrob(items.pts)
maxWidth = grid::unit.pmax(ggpeeps$widths[2:5], ggitems$widths[2:5])
ggpeeps$widths[2:5] <- as.list(maxWidth)
ggitems$widths[2:5] <- as.list(maxWidth)
grid.arrange(ggpeeps, ggitems, ncol=1,
heights=c(3, 2),
top=paste("Persons and Items for", params$themeas))
The only thing I can think of is the gridArrange code changed. Can anyone tell me how I can make the present plot look like the 2021 version?
Many thanks.
Try using the patchwork
package instead. It automatically align axis among other cool features.
library(patchwork)
patchwork::wrap_plots(
ncol=1,
heights = c(3,2),
peeps.hist,
items.pts
)