首页 > 解决方案 > R:游泳图包注释

问题描述

我有一个使用 R 中的游泳图包构建的游泳图,并希望使用来自 data.frame 的另一个变量的因子/文本来注释每个条形图,该变量属于相应的 id/患者。

我无法共享数据,但这是我目前正在处理的代码,最后一行是我未能注释(Best.response.1 是相应文本变量的名称)。“id”是患者变量,“daysuntildeath”是时间变量。

plot3 <- swimmer_plot(df=data ,id='id',end='daysuntildeath',
                      width=.85, name_fill="Drug.1", id_order="Drug.1") +
    scale_fill_manual(name="Drug.1", values = c("coral1", "lavenderblush4", "maroon4","springgreen3" , "springgreen4", "yellowgreen", "dodgerblue2"))
##################### adding symbols for censoring #############################
### censoring
plot4 <- plot3 + swimmer_points(df_points=
                      data,id='id',time='daysuntildeath',name_shape =
                      'event',size=2.5,fill='white',col='black')
p4_new = plot4 + scale_y_discrete(expand = c(0.15,0)) + labs(y = "Days until death") +
  geom_text(x = "id", y = "daysuntildeath", aes(label = "Best.response.1"), vjust = -0.2, colour = "black")

编辑:无法重命名患者变量/刻度线。id 必须是可见的,只能添加注释。

标签: rggplot2annotate

解决方案


总结一下要点:

  • 在最后一行,geom_text使用函数中的xandy参数aes()
  • 标准中通常不引用变量名ggplot2

所以最后一行代码应该是:

p4_new = plot4 + scale_y_discrete(expand = c(0.15,0)) + 
   labs(y = "Days until death") +
   geom_text(aes(x = id, y = daysuntildeath, label = Best.response.1), 
             vjust = -0.2, colour = "black")

推荐阅读