ggplot:将多个具有相同绘图区域大小的面板组合在一起

问题描述 投票:0回答:1

我正在尝试将三个图合并为一个图的三个面板。由于三个面板在 x 轴上共享相同的数据,因此我想删除图 1 和 2 的 x 轴和标签。但是,我遇到了一个问题,即图 1 和 2 的绘图区域要大得多比图 3 的情况要多。我该如何解决这个问题?

library(ggplot2)
library(ggpubr)

# Generate synthetic data
set.seed(123)
df1 <- data.frame(x = 1:10, y = rnorm(10))
df2 <- data.frame(x = 1:10, y = rnorm(10))
df3 <- data.frame(x = 1:10, y = rnorm(10))

# Create plots
p1 <- ggplot(df1, aes(x, y)) + geom_point() + labs(title = "Plot 1")
p2 <- ggplot(df2, aes(x, y)) + geom_point() + labs(title = "Plot 2")
p3 <- ggplot(df3, aes(x, y)) + geom_point() + labs(title = "Plot 3")

# Arrange plots using ggarrange
ggarrange(
  p1 + rremove(c("x.text")) + rremove("xlab"),
  p2 + rremove("x.text") + rremove("xlab"),
  p3,
  labels = c("A", "B", "C"),
  common.legend = TRUE,
  legend = "bottom",
  nrow = 3
)

r ggplot2 plot
1个回答
0
投票

您可以使用

facet_
 中的 
ggplot2

功能
library(dplyr)
library(ggplot2)

set.seed(123)
df1 <- data.frame(x = 1:10, y = rnorm(10)) %>% mutate(facet = "A")
df2 <- data.frame(x = 1:10, y = rnorm(10)) %>% mutate(facet = "B")
df3 <- data.frame(x = 1:10, y = rnorm(10)) %>% mutate(facet = "C")

df <-
  bind_rows(df1,df2,df3)

df %>% 
  ggplot(aes(x, y)) + geom_point() +
  facet_wrap(facets = vars(facet),ncol = 1)+
  theme(panel.spacing = unit(0,'lines'))

© www.soinside.com 2019 - 2024. All rights reserved.