簡體   English   中英

在 ggplot2 中標記圖例並在 R 中標記 ggfortify

[英]Labelling the legend in ggplot2 and ggfortify in R

我繪制了一個圖形並想自定義該圖的圖例。 我將感謝所有幫助。 謝謝!

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沒有自己的庫。 有關更多信息,請參閱此已關閉問題

我使用此處找到的生存分析示例對您的問題進行了輕微編輯,以包含可重現的代碼。 自定義繪圖的示例(重命名圖例和顏色標簽):

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)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM