Center facets in ggplot2
Asked Answered
V

1

6

I'm plotting geom_smooths in facets grouped by size:

library(ggplot2)
ggplot(df,
       aes(x = pos, y = mean_ratio_f ))+
  geom_smooth(aes(group = factor(size)), method = "lm", se = FALSE, linewidth = 0.5) +
  # facets:
  facet_wrap(. ~ size, scales = 'free_x')+
  labs(x ="X", 
       y = "Y")

Unfortunately the last three facets (for size groups 23, 24,and 25) are aligned to the left margin so that there is a gap to their right (which also creates the impression of the whole plot being tilted to the right!):

enter image description here

It appears to me that the issue can be solved by centering the three facets in question (but maybe there are other solutions as well). How can the factes be rearranged so that the last three facets are centered?

Data:

df <- structure(list(size = c(3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 
                              8L, 8L, 9L, 9L, 10L, 10L, 11L, 11L, 12L, 12L, 13L, 13L, 14L, 
                              14L, 15L, 15L, 16L, 16L, 17L, 17L, 18L, 18L, 19L, 19L, 20L, 20L, 
                              21L, 21L, 22L, 22L, 23L, 23L, 24L, 24L, 25L, 25L), pos = c(1.5, 
                                                                                         2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 
                                                                                         2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 
                                                                                         2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 2), mean_ratio_f = c(527.899043866778, 
                                                                                                                                                              1223.75592041265, 26.7055556681507, 1014.99764633205, 6.47082070497567, 
                                                                                                                                                              863.659744048962, 3.81972089409093, 777.045156006896, 2.46140197567771, 
                                                                                                                                                              745.040410893806, 2.22400421369641, 759.114492391129, 2.13729390098214, 
                                                                                                                                                              687.177457687369, 1.98034033753045, 778.931235189388, 1.90373974226176, 
                                                                                                                                                              718.311850673966, 1.80384197110368, 825.996874022512, 1.81708729221153, 
                                                                                                                                                              784.264857079573, 1.7777262939807, 752.39972151211, 1.76691331278538, 
                                                                                                                                                              860.318640599953, 1.75527539730966, 869.777351603508, 1.74520729149527, 
                                                                                                                                                              880.417441527199, 1.73611317639682, 780.755824759386, 1.78837402005967, 
                                                                                                                                                              868.750440691095, 1.70425949150671, 804.161284483241, 1.70130414461642, 
                                                                                                                                                              827.894751207786, 1.6956455656474, 846.217696086233, 1.6805039077424, 
                                                                                                                                                              796.011388849723, 1.65481637360088, 811.918292989823, 1.67084107927763, 
                                                                                                                                                              920.002748174406), mean_ratio_f_log = c(6773.00321795844, 17499.3396876788, 
                                                                                                                                                                                                      91.9407050566451, 14640.9637434847, 0.961390621510839, 12925.4581530315, 
                                                                                                                                                                                                      0.962105282138507, 11965.2882380283, 0.959588977914962, 11925.034356026, 
                                                                                                                                                                                                      0.95894420256844, 12389.1090131876, 0.962673236418588, 10291.5804363065, 
                                                                                                                                                                                                      0.961229361905838, 13564.6305043359, 0.959542208244426, 11807.7801051298, 
                                                                                                                                                                                                      0.958279155901719, 13222.8288829741, 0.960694717000605, 14050.9037663119, 
                                                                                                                                                                                                      0.959865919899295, 13196.4602878018, 0.960818003520457, 18197.3072369647, 
                                                                                                                                                                                                      0.959524692210418, 16167.0124087112, 0.962044614557777, 19156.2703675997, 
                                                                                                                                                                                                      0.958319770694746, 12192.6024672023, 0.96568915213801, 14355.9254483709, 
                                                                                                                                                                                                      0.957678872168589, 12384.5704259404, 0.956930859337691, 15515.5122785017, 
                                                                                                                                                                                                      0.964217350399733, 14886.2543318109, 0.958708854899801, 12105.1755086371, 
                                                                                                                                                                                                      0.959842413426268, 12265.603237096, 0.954623252993519, 15048.4316206923
                                                                                                                                                              )), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, 
                                                                                                                                                                                                                                        -46L), groups = structure(list(size = 3:25, .rows = structure(list(
                                                                                                                                                                                                                                          1:2, 3:4, 5:6, 7:8, 9:10, 11:12, 13:14, 15:16, 17:18, 19:20, 
                                                                                                                                                                                                                                          21:22, 23:24, 25:26, 27:28, 29:30, 31:32, 33:34, 35:36, 37:38, 
                                                                                                                                                                                                                                          39:40, 41:42, 43:44, 45:46), ptype = integer(0), class = c("vctrs_list_of", 
                                                                                                                                                                                                                                                                                                     "vctrs_vctr", "list"))), row.names = c(NA, -23L), class = c("tbl_df", 
                                                                                                                                                                                                                                                                                                                                                                 "tbl", "data.frame"), .drop = TRUE)) 
Vasculum answered 10/1, 2023 at 7:49 Comment(4)
Even if you cut of the last three facets it's look tilted to the right - at least when I look at it. I don't think that it would help to align the last three facets to the center. It is some other kind of optical illusion that makes the diagram tilted.Flight
Interesting question! Not clear if you want to centre the last row or remove the optical illusion.Viera
RelevantRobber
ggh4x::facet_manual is not really working here it seems since the y coordinates of the last row are creating an empty space between the first and second plot of every other rowChlamys
S
5

You can set the layout with ggh4x::facet_manual() and then manually adjust any spacing in the gtable that seem superfluous to you.

library(ggplot2)
library(ggh4x)

# Data omitted from reprex for brevity, but taken from question
# df <- structure(...)

# Create layout
design <- matrix(
  c(1:20, NA, 21:23, NA),
  5, 5, byrow = TRUE
)

# Make plot
p <- ggplot(df, aes(x = pos, y = mean_ratio_f ))+
  geom_smooth(aes(group = factor(size)), 
              method = "lm", se = FALSE, linewidth = 0.5) +
  facet_manual(vars(size), design = design, scales = "free_x") +
  labs(x ="X", y = "Y")

# Convert to gtable
gt <- ggplotGrob(p)
#> `geom_smooth()` using formula = 'y ~ x'

# Set some widths to zero
# I don't know of a programmatic way to get the right indices
gt$widths[8] <- unit(0, "cm")

# Plotting
grid::grid.newpage(); grid::grid.draw(gt)

Created on 2023-01-10 with reprex v2.0.2

Disclaimer: I'm the author of ggh4x

Stir answered 10/1, 2023 at 9:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.