What Im trying to do is create a barplot and instead of showing the x-axis labels, I want to try and replace the labels with individual plots. Hopefully my example and attempted solution below will explain the problem.
To begin, I create a barplot, and then create multiple individual plots to display on the x-axis like so:
library(ggplot)
# create barplot ----------------------------------------------------------
df <- data.frame(vals = c(10, 5, 18),
name = c("A", "B", "C"))
bp <- df %>%
ggplot() +
geom_bar(aes(x = name, y = vals), stat = "identity") +
xlab("") +
theme_bw() +
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank())
# create plots to use as x-axis --------------------------------------------
p1 <- ggplot(df, aes(x = vals, y = vals)) + geom_point() + theme_bw() +
theme(axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())
p3 <- p2 <- p1
# turn into list of plots
myList <- list(p1, p2, p3)
Attempted solution:
My attempted solution was to use the patchwork package to replace the x-axis labels with the individual plots, like so:
library(patchwork)
# setting positions manually
design <- c(
area(1, 1, 4, 4),
area(5,1),
area(5,3),
area(5,4)
)
bp + myList + plot_layout(design = design)
But as you can see, this doesn't align the individual plots under the corresponding bars. The issue of alignment is also compounded if there are more than 3 bars used in the barplot.
Additionally, having to set the positions manually using the patchwork::area function isn't ideal.
Is it possible to create a barplot with the x-axis displaying a bunch of individual plots where I don't have to position them manually?

