Reputation: 2890
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?
Upvotes: 1
Views: 106
Reputation: 124463
One option to achieve your desired result via patchwork
would be to
geom_blank(aes(x=name))
(to get the same axis as in your barplot)annotation_custom
where I make use of purrr::map2
theme_void
patchwork
to glue the "axis plot" to your barchartlibrary(ggplot2)
library(patchwork)
width <- .9 # Default width of bars
p_axis <- ggplot(df) +
geom_blank(aes(x = name)) +
purrr::map2(myList, seq_along(myList), ~ annotation_custom(ggplotGrob(.x), xmin = .y - width / 2, xmax = .y + width / 2)) +
theme_void()
library(patchwork)
bp / p_axis + plot_layout(heights = c(4, 1))
Upvotes: 2