R语言 ggplot2中的中心面

cnwbcb6i  于 2023-01-15  发布在  其他
关注(0)|答案(1)|浏览(273)

我在按size分组的面中绘制geom_smooth

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")

不幸的是,最后三个面(对于size组23、24和25)与左边距对齐,因此在它们的右边有一个间隙(这也造成了整个图向右倾斜的印象!):

在我看来,这个问题可以通过集中讨论中的三个方面来解决(但也许还有其他解决方案)。如何重新安排这些方面,使最后三个方面集中
数据:

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))
dtcbnfnu

dtcbnfnu1#

您可以使用ggh4x::facet_manual()设置布局,然后手动调整gtable中任何看起来多余的间距。

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)

创建于2023年1月10日,使用reprex v2.0.2
免责声明:我是ggh4x的作者

相关问题