Home > Software design >  Replace barplot x-axis labels with plots in R?
Replace barplot x-axis labels with plots in R?

Time:11-08

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)

This looks like: example barplot

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?

CodePudding user response:

One option to achieve your desired result via patchwork would be to

  1. create a ggplot using geom_blank(aes(x=name)) (to get the same axis as in your barplot)
  2. add the plots to be used as axis labels via annotation_custom where I make use of purrr::map2
  3. remove all non-data ink via theme_void
  4. use patchwork to glue the "axis plot" to your barchart
library(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))

  • Related