R语言 拼接不减少两个ggplot2之间白色

qlzsbp2j  于 2023-09-27  发布在  其他
关注(0)|答案(2)|浏览(75)

我用df1df2创建了这个图

我尝试了很多方法来减少红色包围的白色空间。我如何才能做到这一点?有一个类似的thread on SO,我遵循,但这并没有解决它:
第一:

who1pred <- ggplot(filter(df1), 
                   aes(x = ki67, y = pred, color = time, fill = time)) +
  geom_ribbon(aes(ymin = lower, ymax = upper), color = NA) +
  geom_line() +
  scale_x_continuous(name = "",
                     breaks = seq(0, 50, 10)) +
  coord_cartesian(xlim = c(0, 50))  + 
  theme(axis.ticks.x = element_blank(),
        axis.text.x = element_blank(),
    #plot.margin = unit(c(5.5, 5.5, 0, 5.5), "pt")
        )

who1dist <- ggplot(filter(df2), 
                   aes(x = Ki67)) +
  geom_histogram() +
  scale_x_continuous(name = "",
                     breaks = seq(0, 50, 10)) +
  coord_cartesian(xlim = c(0, 50)) #+
  #theme(plot.margin = unit(c(0, 5.5, 0, 5.5), "pt"))

基本情节:

who1pred / who1dist +
  plot_layout(ncol = 1, heights = c(1, .4))

我尝试了以下方法(以及其中的一些变体)

who1pred / plot_spacer() / who1dist +
  plot_layout(ncol = 1, heights = c(1, 0, .4),
              guides = "collect")

我试着在ggplots中调整theme(plot.margin = ..)
我还尝试了以下在plot.margin中使用不同值的方法。

who1pred / who1dist +
  plot_layout(ncol = 1, heights = c(1, .4) & theme(plot.margin = c(0, 0, 0, 0)))

有什么办法吗?

df1 <- structure(list(WHO = c("2", "2", "3", "1", "1", "2", "2", "3", 
                              "1", "3"), ki67 = c(94, 64, 51, 15, 18, 74, 93, 45, 65, 62), 
                      pred = c(0.464410116302291, 0.897401711241776, 0.700242375617693, 
                               0.358678057582871, 0.390573594975723, 0.829113422265248, 
                               0.48802465997491, 0.666022132399912, 0.540540217190029, 0.699322187068211
                      ), time = c("120", "120", "120", "120", "120", "120", "120", 
                                  "120", "120", "60"), lower = c(0.0137577850103707, 0.785671003373972, 
                                                                 0.540479287582779, 0.282505841174618, 0.282542223489564, 
                                                                 0.654223374533307, 0.0466809675600842, 0.490877830284619, 
                                                                 0, 0.550598433238934), upper = c(0.915062447594212, 1, 0.860005463652608, 
                                                                                                  0.434850273991124, 0.498604966461882, 1, 0.929368352389736, 
                                                                                                  0.841166434515205, 1, 0.848045940897488)), row.names = c(NA, 
                                                                                                                                                           -10L), class = c("tbl_df", "tbl", "data.frame"))
df2 <- dput(sample_n(select(d_WHO, Ki67, WHO), 10))
structure(list(Ki67 = c(1, 2, 5, 11, 1, 1, 8, 1, 4, 2), WHO = c("1", 
                                                                "1", "2", "2", "1", "1", "2", "1", "1", "2")), row.names = c(NA, 
                                                                                                                             -10L), class = "data.frame")
jljoyd4f

jljoyd4f1#

你就快到了。此时,您已将x轴标题设置为""。这需要与任何其他轴标题一样高的高度。您只需要在第一个图上设置axis.title.x = element_blank()即可完全删除轴标题。

z2acfund

z2acfund2#

你可以在heights参数中使用一个负的调整来将它们绘制得更接近getter,如下所示:

library(patchwork)
library(ggplot2)
library(dplyr)

who1pred <- ggplot(filter(df1), 
                   aes(x = ki67, y = pred, color = time, fill = time)) +
  geom_ribbon(aes(ymin = lower, ymax = upper), color = NA) +
  geom_line() +
  scale_x_continuous(name = "",
                     breaks = seq(0, 50, 10)) +
  coord_cartesian(xlim = c(0, 50))  + 
  theme(axis.ticks.x = element_blank(),
        axis.text.x = element_blank(),
        #plot.margin = unit(c(5.5, 5.5, 0, 5.5), "pt")
  )

who1dist <- ggplot(filter(df2), 
                   aes(x = Ki67)) +
  geom_histogram() +
  scale_x_continuous(name = "",
                     breaks = seq(0, 50, 10)) +
  coord_cartesian(xlim = c(0, 50)) #+
#theme(plot.margin = unit(c(0, 5.5, 0, 5.5), "pt"))

who1pred / plot_spacer() / who1dist +
  plot_layout(ncol = 1, heights = c(1, -0.1, 0.4),
              guides = "collect")
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

这是最初的情节:

创建于2023-09-13带有reprex v2.0.2

相关问题