2

I have my an empty panel in my facetted ggplot. I would like to insert my standalone plot into this. Is this possible? See below for example code.

I found a possible solution Here, but can't get it to 'look nice'. To 'look nice' I want the standalone plot to have the same dimensions as one of the facetted plots.

library(ggplot2)
library(plotly)
data("mpg")

first_plot = ggplot(data = mpg, aes(x = trans, y = cty)) + 
  geom_point(size= 1.3)

facet_plot = ggplot(data = mpg, aes(x = year, y = cty)) +
  geom_point(size = 1.3) +
  facet_wrap(~manufacturer)
facet_plot  # room for one more panel which I want first_plot to go?

# try an merge but makes first plot huge, compared with facetted plots.
subplot(first_plot, facet_plot, which_layout = 2)
Cyrillm_44
  • 701
  • 3
  • 17
  • 1
    Have you tried this solution using `gtable` ? https://stackoverflow.com/questions/22450765/how-to-use-empty-space-produced-by-facet-wrap – Ronak Shah Aug 10 '21 at 01:03

1 Answers1

1

Besides the options to manipulate the gtable or using patchwork one approach to achieve your desired result would be via some data wrangling to add the standalone plot as an additional facet. Not sure whether this will work for your real data but at least for mpg you could do:

library(ggplot2)
library(dplyr)


mpg_bind <- list(standalone = mpg, facet = mpg) %>% 
  bind_rows(.id = "id") %>% 
  mutate(x = ifelse(id == "standalone", trans, year),
         facet = ifelse(id == "standalone", "all", manufacturer),
         facet = forcats::fct_relevel(facet, "all", after = 1000)) 

ggplot(data = mpg_bind, aes(x = x, y = cty)) +
  geom_point(size = 1.3) +
  facet_wrap(~facet, scales = "free_x")

stefan
  • 90,330
  • 6
  • 25
  • 51