[英]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.