簡體   English   中英

ggplot2-編輯圖例

[英]ggplot2 - Edit Legend

我正在研究游泳者圖以獲取一組數據。 我正在嘗試根據患者是否仍在接受治療對每個條進行顏色編碼。 我已經對其進行了格式化,以使每個條形都根據其打開或關閉處理進行着色。 如何編輯圖例,以便可以更改標題並指定“啟用處理”或“禁用處理”而不是0或1?

示例數據幀TonT1:

Patient ID        Time1(months)     On Treatment (1 for yes, 0 for no)
1                     6                    1
2                     4.7                  0
3                     2.3                  1
4                     9.7                  1

...等

我的代碼如下:

ggplot(data=TonT1, 
aes(x=reorder(Patient.ID, Time1), y=Time1, fill = factor(Off.Treatment))) 
+ ylim(0,15) + ylab("Time on Therapy") + xlab("Patient ID") 
+ theme_classic() + geom_bar(stat="identity", width = 0.5)

只需指定填充的標題即可。

dat <- data.frame("Patient ID" = c(1,2,3,4), "Time1" = c(6, 4.7, 2.3, 9.7), "Off.Treatment" = c(1,0,1,1))

ggplot(data=dat, 
       aes(x=reorder(Patient.ID, Time1), y=Time1, fill = factor(Off.Treatment))) + 
  ylim(0,15) + ylab("Time on Therapy") + xlab("Patient ID") + 
  theme_classic() + geom_bar(stat="identity", width = 0.5) +
  guides(fill = guide_legend(title = "On Treatment"))

暫無
暫無

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

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