I am attempting to accomplish two tasks:
Cohesively combine two figures that share the same y axis, but one which has a categorical x axis variable and the other that has a continuous x axis variable. I would like to display them as contiguous, only separated by a solid black line (i.e. the right edge of the left plot and the left edge of the right plot).
Modify freely the dimensions of the figures, so that I can i. extend the x axis on the left figure to better demonstrate the spread of the data, and to ii. idealize the ratio of the size of the two figures.
Below is my attempt:
#libraries used:
library(ggplot2)
library(dplyr)
#Pulling in example dataset:
data_1 <- iris
#Building my left figure, which has a continuous x and y axis, and establishing y axis limits to match between the two figures:
object_1 <- ggplot(data_1, aes(x = Sepal.Width, y = Sepal.Length)) geom_point() ylim(0, 10)
#Building my second data table:
data_2 <- iris %>% group_by(Species) %>% summarize(av_petal_length = mean(Petal.Length))
#Building my right hand figure, with empty y axis titles and text to provide space to combine the two figures on the left y axis:
object_2 <- ggplot(data_2, aes(x = Species, y = av_petal_length)) geom_point() ylim(0, 10)
theme(axis.title.y = element_blank(),
axis.text.y = element_blank())
#Attempt to grid.arrange:
grid.arrange(object_1, object_2, nrow = 1)
As you can see, a simple grid.arrange does not combine them completely. I have attempted to modify the panel margins in the two figures by tinkering with plot.margin() under theme(), but this requires a lot of tinkering and if the figures get resized at all the relationship between the two figures can become distorted. Is it possible to cleanly, simply combine these two figures into one cohesive rectangle, separated by a line, and to manually modify the dimensions of the figures?
CodePudding user response:
Below, we're using seperate themes for the left and right plots that delete the relevant plot margins and the y-axis of the right plot.
I'm sure you can do it with grid.arrange() too, but {patchwork} allows you to set figure widths as well.
library(ggplot2)
library(dplyr)
library(patchwork)
# As before
data_1 <- iris
object_1 <- ggplot(data_1, aes(x = Sepal.Width, y = Sepal.Length)) geom_point() ylim(0, 10)
data_2 <- iris %>% group_by(Species) %>% summarize(av_petal_length = mean(Petal.Length))
object_2 <- ggplot(data_2, aes(x = Species, y = av_petal_length)) geom_point() ylim(0, 10)
# Remove relevant margins from theme, including y-axis elements on the right
theme_left <- theme(plot.margin = margin(5.5, 0, 5.5, 5.5))
theme_right <- theme(plot.margin = margin(5.5, 5.5, 5.5, 0),
axis.ticks.length.y = unit(0, "pt"),
axis.title.y = element_blank(),
axis.text.y = element_blank())
black_line <- annotate("segment", x = Inf, xend = Inf, y = -Inf, yend = Inf, size = 2)
# Patchwork everything together
(object_1 theme_left black_line)
(object_2 theme_right)
plot_layout(widths = c(2, 1))

Created on 2022-02-01 by the reprex package (v2.0.1)

