在ggplot中沿coefplot轴按组增加空白

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

使用下面的数据,如何编辑 coefplot 以按组增加沿 y 轴的空白?

到目前为止,我只有一个使用 geom_point() 和 geom_errorbar() 的简单 ggplot 代码结构,但只能更改颜色来指示 3 个组。有没有办法手动增加空白以进一步增加 3 组之间的分隔?

编辑:这是我现在使用的一些基本代码:

library(tidyverse)

ggplot(dtest, aes(x = coef, y = modelfactor, color = cohort,
                  xmin = ci_lower, xmax = ci_upper)) +
  geom_point() + 
  geom_errorbar(width = 0) +
  labs(y = NULL) +
  theme_light() +
  theme(legend.position = "none")
dtest <- structure(list(coef = structure(c(0.0119970114901662, -0.0280775502324104, 
-0.0579900853335857, 0.00396365346387029, 0.090943768620491, 
0.110191114246845, -0.203988239169121, -0.132528975605965, 0.267586797475815
), label = "Coefficient", format.stata = "%9.0g"), stderr = structure(c(0.218027532100677, 
0.14348241686821, 0.169379606842995, 0.225308120250702, 0.119860216975212, 
0.164130866527557, 0.0966519564390182, 0.0921176299452782, 0.122128166258335
), label = "Standard error", format.stata = "%9.0g"), ci_lower = structure(c(-0.424432516098022, 
-0.315288811922073, -0.396917909383774, -0.447563916444778, -0.14968566596508, 
-0.219013690948486, -0.396004229784012, -0.31550931930542, 0.0250644627958536
), label = "95% confidence interval (lower bound)", format.stata = "%9.0g"), 
    ci_upper = structure(c(0.44842654466629, 0.259133726358414, 
    0.280937731266022, 0.455491214990616, 0.331573218107224, 
    0.439395934343338, -0.0119722569361329, 0.0504513755440712, 
    0.510109126567841), label = "95% confidence interval (upper bound)", format.stata = "%9.0g"), 
    N = structure(c(73, 73, 74, 70, 66, 67, 105, 106, 108), label = "Number of observations", format.stata = "%10.0g"), 
    r2 = structure(c(0.227822259068489, 0.428009748458862, 0.0902877897024155, 
    0.217700824141502, 0.38048067688942, 0.287189871072769, 0.128605201840401, 
    0.159074306488037, 0.243677839636803), label = "R-squared", format.stata = "%9.0g"), 
    model = structure(c("Group3 Oldest", "Group3 Middle", 
    "Group3 Youngest", "Group2 Oldest", "Group2 Middle", 
    "Group2 Youngest", "Group1 Oldest", "Group1 Middle", "Group1 Youngest"
    ), label = "Model name", format.stata = "%17s"), group = c("Group1", 
    "Group1", "Group1", "Group2", "Group2", "Group2", "Group3", 
    "Group3", "Group3"), cohort = c("Cohort12", "Cohort12", "Cohort12", 
    "Cohort9", "Cohort9", "Cohort9", "Cohort0", "Cohort0", "Cohort0"
    ), modelfactor = structure(1:9, levels = c("Group3 Oldest", 
    "Group3 Middle", "Group3 Youngest", "Group2 Oldest", 
    "Group2 Middle", "Group2 Youngest", "Group1 Oldest", "Group1 Middle", 
    "Group1 Youngest"), class = "factor")), row.names = c(NA, 
-9L), class = c("tbl_df", "tbl", "data.frame"))
r ggplot2
1个回答
0
投票

方面可以是强调分组的好方法:

ggplot(dtest, aes(x = coef, y = modelfactor, color = cohort,
                  xmin = ci_lower, xmax = ci_upper)) +
  geom_point() + 
  geom_errorbar(width = 0) +
  labs(y = NULL) +
  theme_light() +
  theme(legend.position = "none") +
  facet_grid(vars(cohort), scales = "free_y")

enter image description here

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.