r - 在 ggplot2 中标记图例,在 R 中标记 ggfortify

标签 r ggplot2 survival-analysis ggfortify

我绘制了一个图表并想自定义该图表的图例。我将感谢所有帮助。谢谢!

library("survival")
library("ggplot2")
library("ggfortify")

data(lung)
lung$SurvObj <- with(lung, Surv(time, status == 2))
km.by.sex <- survfit(SurvObj ~ sex, data = lung, conf.type = "log-log")

gender.plot <- autoplot(km.by.sex)
gender.plot <- gender.plot + 
  ggtitle("Gender based Survival (1=male, 2=female)") +
  labs(x = "Time", y = "Survival Probability") 
print(gender.plot)

最佳答案

我在自定义 ggfortify 图时遇到了类似的问题 - 我不确定这个问题在问什么,但我将假设您想从 ggfortifyautoplot。要快速回答这个问题 - autoplot 可以使用典型的 ggplot 函数进行自定义操作,因为它是一个 ggplot 对象。这应该是修改图例的方法,autoplot 没有自己的库。参见 this closed issue获取更多信息。

我使用发现的生存分析示例对您的问题进行了轻微编辑以包含可重现的代码 here .自定义绘图的示例(重命名图例和颜色标签):

gender.plot <- autoplot(km.by.sex)
gender.plot <- gender.plot + 
  ggtitle("Gender based Survival") +
  labs(x = "Time", y = "Survival Probability") +
  guides(fill=FALSE) +
  labs(colour = "Gender") +
  scale_color_manual(labels = c("Male", "Female"), values = c(1, 2))
print(gender.plot)

enter image description here

关于r - 在 ggplot2 中标记图例,在 R 中标记 ggfortify,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46780863/

相关文章:

r - 在R中, “loaded via a namespace (and not attached)”是什么意思?

r - 在 R Shiny 中动态调整 ggplot2 图形的大小

r - 杀死幽灵 NULL 控制台输出?

string - 匹配 R 中字符串的一部分

r - 如何向 ggplot2 主题添加额外参数?

r - 讨厌的饼图箭头

r - 分层考克斯模型的生存

r - 计算特定时间间隔内 KM 图中的幸存者数量

python - 将包含 16k 字典的字典更改为 Pandas Dataframe

r - 使用geom_point()避开位置,x =连续,y =因数