如何使 y 轴上的线彼此更靠近以及与 x 轴更靠近

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

这个问题涉及与我上一个问题相同的数据和图表:如何将 y 轴上的线彼此更靠近地定位,但问题本身有点不同。

数据为:

df <- structure(list(vec = c("PC", "DEFAULT", "INFORMED"), mean = c(1.34944359241928, 
                                                      1.36249329506777, 1.34671188869646), sd = c(0.57779881866326, 
                                                                                                  0.537279303541924, 1.53585580464849), min = c(0.196903771571785, 
                                                                                                                                                0.28781509871908, -1.66860228474139), `0.5quant` = c(1.35295469982876, 
                                                                                                                                                                                                     1.36643973099316, 1.34687516700723), max = c(2.48177705326348, 
                                                                                                                                                                                                                                                  2.41483607230639, 4.36109982194179), mode = c(NA_real_, NA_real_, 
                                                                                                                                                                                                                                                                                                NA_real_), kld = c(1.9807860333589e-08, 2.97333113261951e-08, 
                                                                                                                                                                                                                                                                                                                   3.91753938449056e-10), ID = 1:3), row.names = c(NA, -3L), class = "data.frame")

我想将其绘制为:

yaxis <- c(
  "Ppppppppp ppppppp\npppp pp pppppp\n(ppp)",
  "Uuuuuuuuu uuuuu","Iiiiiii iiiii\nii Iiiiii (iiii)") 



ggplot(data = df, aes(x = mean, y = factor(ID))) + geom_point() + geom_errorbarh(aes(xmin = min, xmax = max), height = .1) + geom_vline(xintercept = 0, linetype = 2) + scale_x_continuous(breaks = seq(-4, 4.5, 0.5)) + scale_y_discrete(labels = yaxis) + theme(axis.text.y = element_text(hjust = .5))

enter image description here

正如我在图片中所写的,我想将三条线放置得彼此更靠近,并且与 x 轴更靠近。

在上一个问题中,Limey 建议:

    + scale_y_discrete(labels = yaxis, expand = expansion(add = 2))

但这会增加线条和 x 轴之间的距离。

r ggplot2 plot
1个回答
0
投票

在将 R 绘图窗口导出到文件之前调整其大小,或者在

ggsave
中指定宽度和高度。

ggplot(data = df, aes(x = mean, y = factor(ID))) + 
  geom_point() + 
  geom_errorbarh(aes(xmin = min, xmax = max), height = .1) + 
  geom_vline(xintercept = 0, linetype = 2) + 
  scale_x_continuous(breaks = seq(-4, 4.5, 0.5)) + 
  scale_y_discrete(labels = yaxis) + 
  theme(axis.text.y = element_text(hjust = .5))

ggsave(height=1.5, width=5, file="RPlot.jpg")

enter image description here

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