将 y 轴上的组名称以粗体添加到水平误差条形图中,更改标签之间的空格并添加额外的文本列

Adding the name of a group on the y axis in bold to a horizontal error bar plot, changing spaces in between labels ánd add an extra text column

我得到了 2 个回归模型的数据(都得到了一个 CI 相应下限和上限的估计值)。两个模型都包括 3 个变量(年龄、性别和吸烟状况)。变量 term(对应于模型中的变量)已排序,因此它按我指定的顺序显示。

数据如下:

library(tidyverse)
library(ggplot2)

mydata <- structure(list(term = structure(c(1L, 1L, 2L, 2L, 3L, 4L, 4L, 
5L, 5L), .Label = c("Age (years)", "Sex (male)", "Never smoking (reference)", 
"Current smoking", "Former smoking", ">90 (reference)", "60-89", 
"<60"), class = c("ordered", "factor")), estimate = c(1.5, 2.2, 
0.7, 1.8, 1, 1.5, 2.2, 0.7, 1.6), conf_low = c(1.3, 1.8, 0.9, 
1, 1, 1.3, 1.8, 0.9, 1), conf_high = c(1.7, 2.6, 0.5, 2.6, 1, 
1.7, 2.6, 0.5, 2.4), model = structure(c(1L, 2L, 1L, 2L, NA, 
1L, 2L, 1L, 2L), .Label = c("Model 1", "Model 2"), class = c("ordered", 
"factor")), label = structure(c(3L, 6L, 1L, 5L, 2L, 3L, 6L, 1L, 
4L), .Label = c("0.7 (0.9-0.5)", "1.0 (1.0-1.0)", "1.5 (1.3-1.7)", 
"1.6 (1.0-2.4)", "1.8 (1.0-2.6)", "2.2 (1.8-2.6)"), class = "factor")), row.names = c(NA, 
-9L), class = c("tbl_df", "tbl", "data.frame"))

head(mydata)
# A tibble: 6 x 6
  term                      estimate conf_low conf_high model   label        
  <ord>                        <dbl>    <dbl>     <dbl> <ord>   <fct>        
1 Age (years)                    1.5      1.3       1.7 Model 1 1.5 (1.3-1.7)
2 Age (years)                    2.2      1.8       2.6 Model 2 2.2 (1.8-2.6)
3 Sex (male)                     0.7      0.9       0.5 Model 1 0.7 (0.9-0.5)
4 Sex (male)                     1.8      1         2.6 Model 2 1.8 (1.0-2.6)
5 Never smoking (reference)      1        1         1   NA      1.0 (1.0-1.0)
6 Current smoking                1.5      1.3       1.7 Model 1 1.5 (1.3-1.7)

我做了以下情节:

ggplot(data=mydata, 
       aes(x=estimate, 
           y=fct_rev(term), 
           color=model)) + 
  geom_point(position=position_dodge(width=0.3)) + 
  geom_errorbarh(aes(xmin=conf_low, xmax=conf_high, height=0.15), position=position_dodge(width=0.3))

现在我进一步尝试实现我无法完成的两件事:

  1. 我想在 Y 轴绘图中的 Never smoking (reference) 上方添加一个 title/variable 名称,以便在该标签上方显示 吸烟状况 (粗体)。我可以向原始数据框添加另一行,但标题和 'never smoking' 之间的距离会有点大。有没有办法添加标题,或者调整 Y 轴上某些标签之间的特定距离的方法?
  2. 数据集中还有一列label,对应估计和95%CI。我想将其添加为绘图右侧的额外列,以便它们与其对应的高度相同 points/error-bars.

谢谢!

实现所需结果的一个选项是使用 annotation_custom 添加组 header 并使用 geom_text 添加误差线的文本标签:

注意:要将注释放在情节之外,我使用 coord_cartesian(clip = "off")

library(ggplot2)
library(forcats)

ggplot(
  data = mydata,
  aes(
    x = estimate,
    y = fct_rev(term),
    color = model
  )
) +
  geom_point(position = position_dodge(width = 0.3)) +
  geom_errorbarh(aes(xmin = conf_low, xmax = conf_high, height = 0.15), position = position_dodge(width = 0.3)) +
  annotation_custom(grob = grid::textGrob(label = "Smoking status", gp = grid::gpar(fontface = "bold"), hjust = 1), xmin = -Inf, xmax = -Inf, ymin = 3.2, ymax = 3.2) +
  geom_text(aes(x = max(conf_high) * 1.05, label = label, group = model), position = position_dodge(width = 0.3), hjust = 0, show.legend = FALSE, color = "black") +
  scale_x_continuous(expand = expansion(mult = c(.05, .3))) +
  coord_cartesian(clip = "off")
#> Warning: Removed 1 rows containing missing values (geom_point).