如何更改 ggplot2 中的 y 尺度和绘图之间的间距?

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

enter image description here 我需要所有 3 个图的 y 轴相同(最大值为 0.6 应该很好),并且我需要将 ΨTort|Ig.Pr 和 ΨTort|Ig.Ab 分组到图 a、ΨIg.NT 和 ΨIg.T 中以及 ΨTort.T 和 ΨTort.NT 一起出现在图 b 中,ΨIg.SP 和 ΨIg.PP 以及 ΨTort.SP 和 ΨTort.PP 出现在图 c 中。 (参见附图...底部的箭头显示应移动/分组得更近以便于比较的项目...顶部的箭头显示应全部相同的比例)

编辑:我使用 chatGPT 来解决比例问题,但它无法将所需的参数更紧密地分组在一起。因此,以下代码生成具有所需比例的图形,但参数仍然均匀分布,即使在 chatGPT 添加的代码中提到将它们分组。

这是我的代码:

`library(ggplot2)
library(cowplot)

# Combine Parameters for Plot A
d.conditional <- data.frame(parameter = c("ΨIg", "ΨTort|Ig.Pr", "ΨTort|Ig.Ab"),
                            estimate = c(0.24, 0.07, 0.19),
                            lower = c(0.18, 0.02, 0.13),
                            upper = c(0.31, 0.20, 0.27))

# Combine Parameters for Plot B
d.tunnel <- data.frame(parameter = c("ΨIg.NT", "ΨIg.T", "ΨTort.NT", "ΨTort.T"),
                       estimate = c(0.10, 0.30, 0.26, 0.12),
                       lower = c(0.04, 0.22, 0.16, 0.07),
                       upper = c(0.22, 0.39, 0.40, 0.19))

# Combine Parameters for Plot C
d.PPmgmt <- data.frame(parameter = c("ΨIg.SP", "ΨIg.PP", "ΨTort.SP", "ΨTort.PP"),
                       estimate = c(0.46, 0.13, 0.09, 0.20),
                       lower = c(0.34, 0.08, 0.04, 0.14),
                       upper = c(0.59, 0.21, 0.19, 0.28))

# Create the plots
figA <- ggplot(data = d.conditional, aes(x = parameter, y = estimate)) +
  geom_errorbar(aes(ymin = upper, ymax = lower), width = 0.15, size = 0.8, color = "black") +
  geom_point(size = 4, shape = 21, fill = "black") +
  ylim(0, 0.6) +     # Set y-axis limit
  theme_bw() +
  theme(text = element_text(family = "Arial", size = 9)) +
  labs(x = "", y = "Estimated occupancy probability")

figB <- ggplot(data = d.tunnel, aes(x = parameter, y = estimate)) +
  geom_errorbar(aes(ymin = upper, ymax = lower), width = 0.3, size = 0.8, color = "black") +
  geom_point(size = 4, shape = 21, fill = "black") +
  ylim(0, 0.6) +     # Set y-axis limit
  theme_bw() +
  theme(text = element_text(family = "Arial", size = 9)) +
  labs(x = "", y = "")

figC <- ggplot(data = d.PPmgmt, aes(x = parameter, y = estimate)) +
  geom_errorbar(aes(ymin = upper, ymax = lower), width = 0.3, size = 0.8, color = "black") +
  geom_point(size = 4, shape = 21, fill = "black") +
  ylim(0, 0.6) +     # Set y-axis limit
  theme_bw() +
  theme(text = element_text(family = "Arial", size = 9)) +
  labs(x = "", y = "")

# Arrange plots using cowplot
combined_plots <- plot_grid(figA, figB, figC, labels = c("a", "b", "c"), ncol = 3)

# Display the combined plot
combined_plots`
r ggplot2
1个回答
0
投票

一个选项是

ggh4x::scale_x_manual
,它允许手动设置离散位置比例的中断位置。在下面的代码中,我将您想要更靠近的类别的分隔符稍微移动了
+/- .1

library(ggplot2)
library(cowplot)
library(ggh4x)

figA <- figA +
  ggh4x::scale_x_manual(
    values = c(1, 2.1, 2.9)
  )

figB <- figB +
  ggh4x::scale_x_manual(
    values = c(1.1, 1.9, 3.1, 3.9)
  )

figC <- figC +
  ggh4x::scale_x_manual(
    values = c(1.1, 1.9, 3.1, 3.9)
  )

combined_plots <- plot_grid(figA, figB, figC,
  labels = c("a", "b", "c"), ncol = 3
)

combined_plots

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