我有一个条形图,其变量标签(其中几个)需要更改。在这里的特定示例中,我有一个变量“ Sputum.Throat”,它表示可能是痰或咽拭子的样本,因此此值的标签应真正显示为“ Sputum / Throat”,甚至是“ Sputum / Throat”拭子”(仅当我可以包装文字时,后者才有效)。到目前为止,我尝试过的语法都无法做到这一点。
这是我的代码:
CultPerf <- data.frame(Blood=ForAnalysis$Cult_lastmo_blood, CSF=ForAnalysis$Cult_lastmo_csf, Fecal=ForAnalysis$Cult_lastmo_fecal, Genital=ForAnalysis$Cult_lastmo_genital, `Sputum-Throat`=ForAnalysis$`Cult_lastmo_sput-throat`, Urine=ForAnalysis$Cult_lastm_urine, `Wound-Surgical`=ForAnalysis$`Cult_lastmo_wound-surg`, Other=ForAnalysis$Cult_lastmo_oth)
CP <- data.table::melt(CultPerf, variable.names("Frequency"))
CP$value <- factor(CP$value, levels=c(">100","50-100","25-50","0-25"))
CP$variable <- factor(CP$variable, levels = c("Other","Wound.Surgical","Urine","Sputum.Throat","Genital","Fecal","CSF","Blood"))
ggplot(data=CP)+
geom_bar(aes(x=variable, fill = value), position="dodge", width = 0.9)+
labs(x="Culture Type", y="Number of Labs", title="Number of Cultures Performed Per Month at Study Hospitals", subtitle="n=140")+
coord_flip()+
theme(legend.title = element_blank(),aspect.ratio = 1.25/1,plot.subtitle=element_text(face="italic",hjust=0.5),plot.title=element_text(hjust=0.5))+
guides(fill = guide_legend(reverse = TRUE))
作为参考,这是它确实产生的成功绘图的副本:
正如我提到的,我要做的就是更改Y轴上各个值的标签。任何建议将不胜感激!
答案 0 :(得分:3)
如果您只想更改该类别的轴标签,请尝试在此行添加
scale_x_discrete(labels=c("Sputum.Throat"="Sputum/Throat"))
确保将它(+
)添加到您的ggplot对象中。
答案 1 :(得分:0)
使用上面@MrFlick的有用建议(谢谢),我在ggplot代码中添加了以下内容,这也给了我第二个标签的文字包装标签:
scale_x_discrete(labels=c("Sputum.Throat"="Sputum/Throat", "Wound.Surgical"="Surgical or \n Other Wound"))+
结果图如下: Revised plot