Wrapping very long plot

Hi there!

I have a very long plot that I'd like to split into several sub-plots to make it readable.
To my data.frame, I added an additional variable to make sub-groups for wrapping and used facet_wrap to make multiple plots. However, due to the 'free_x' scale, the last plot looks weird. I want it to have the same x:y ratio with the space at the right being just blank.

suppressMessages({
  library(tibble)
  library(dplyr)
  library(ggplot2)
})

df <- tibble(x = 1:90,
             y = sample(x = LETTERS, size = 90, replace = TRUE)) %>%
  mutate(facet_group = (x - 1) %/% 20)

ggplot(df) +
  geom_tile(aes(x = x,
                y = 1,
                fill = y)) +
  facet_wrap(facets = vars(facet_group),
             scales = 'free_x',
             ncol = 1) +
  theme_bw() +
  theme(strip.background = element_blank(),
        strip.text.x = element_blank(),
        legend.position = 'None')

Created on 2023-09-15 with reprex v2.0.2

Thanks in advance.

suppressMessages({
  library(tibble)
  library(dplyr)
  library(ggplot2)
})

df <- tibble(x = 1:90,
             y = sample(x = LETTERS, size = 90, replace = TRUE)) %>%
  mutate(facet_group = (x - 1) %/% 20,
         xpcnt = (x-(1+facet_group*20))/(20-1))


ggplot(df) +
  geom_tile(aes(x = xpcnt,
                y = 1,
                fill = y)) +
  facet_wrap(facets = vars(facet_group),
             scales = 'fixed',
             ncol = 1) +
  theme_bw() +
  theme(strip.background = element_blank(),
        strip.text.x = element_blank(),
        legend.position = 'None')

@nirgrahamuk , as a workaround I would rather add NA values at the end of the tibble not to mess up with x-axis labels (they are still meaningful even after wrapping). With the default color scale it assigns grey to NAs so needs to be fixed, but works perfectly with viridis scales.
I actually was wondering if there is a way to do it with the ggplot functionality.

suppressMessages({
  library(tibble)
  library(dplyr)
  library(ggplot2)
  library(viridisLite)
})

df <- tibble(x = 1:90,
             y = sample(x = LETTERS, size = 90, replace = TRUE))

facet_width <- 20
total_width <- max(df$x)

blank_lines <- tibble(x = seq(total_width + 1,
                              total_width + facet_width - total_width%%facet_width,
                              1),
                      y = NA)

df <- bind_rows(df, blank_lines) %>%
  mutate(facet_group = (x - 1) %/% facet_width)


ggplot(df) +
  geom_tile(aes(x = x,
                y = 1,
                fill = y)) +
  facet_wrap(facets = vars(facet_group),
             scales = 'free_x',
             ncol = 1) +
  scale_fill_viridis_d() +
  theme_bw() +
  theme(strip.background = element_blank(),
        strip.text.x = element_blank(),
        legend.position = 'None')

Created on 2023-09-15 with reprex v2.0.2

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.