[英]ggplot2 - Edit Legend
I am working on swimmers plot for a set of data. 我正在研究游泳者图以获取一组数据。 I am trying to color code each bar based on whether or not the patient is still on treatment. 我正在尝试根据患者是否仍在接受治疗对每个条进行颜色编码。 I have formatted it so that each bar is colored based on the if they are on or off treatment. 我已经对其进行了格式化,以使每个条形都根据其打开或关闭处理进行着色。 How do I edit the legend so that I can change the title and specify "on treatment" or "off treatment" instead of 0 or 1? 如何编辑图例,以便可以更改标题并指定“启用处理”或“禁用处理”而不是0或1?
Example data frame TonT1: 示例数据帧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
... etc. ...等
My code is below: 我的代码如下:
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)
Just specify the title for the fill. 只需指定填充的标题即可。
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.