簡體   English   中英

ggplot2 中的中心面

[英]Center facets in ggplot2

我在按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)與左邊距對齊,因此它們的右邊有一個間隙(這也造成了整個 plot 向右傾斜的印象:):

在此處輸入圖像描述

在我看來,這個問題可以通過集中討論的三個方面來解決(但也許還有其他解決方案)。 如何重新排列事實以使最后三個方面居中

數據:

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

您可以使用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-01-10,使用reprex v2.0.2

免責聲明:我是 ggh4x 的作者

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM