R 绘制带有数字标签的多分类柱状图

数据

case	model	studyyear
451	DBN 1   	    T0
121	DBN 1	        T1
227	DBN 1	        T2
460	DBN 2      	T0
231	DBN 2 	    T1
227	DBN 2	        T2
572	Diagnosis	T0
400	Diagnosis	T1
400	Diagnosis	T2

代码

library('ggplot2')
library('reshape2')
predictioncase <- read.csv("predicted cases.csv")  # 数据存储在csv
head(predictioncase)
p = ggplot(predictioncase, aes(x = studyyear,y = case,fill = model))+
  #####这部分的position_dodge(width=0.8)大于宽width = 0.6点,可以使得分组内柱子之间有缝隙,而不是贴合
  geom_bar(stat ="identity",width = 0.6,position = position_dodge(width=0.8))+         
  scale_fill_manual(values = c("#00afbb","#4682b4","#fc4e07"))+                 #########设定颜色
  labs(x = "",y = "")+                        ############坐标标签和图片title
  geom_text(aes(label = case),position=position_dodge(width = 0.9),size = 1.5,vjust = -0.25)+ ###########设置柱子上的标签文字,文字的position_dodge(width=0.5)设置,保证分隔宽度。
  guides(fill = guide_legend(reverse = F))+                  ##############图例顺序反转
  theme(axis.text=element_text(size=6,face = "plain"),
        legend.title = element_blank(),                    ##########图例名称为空
        legend.text = element_text(size = 4, face = "plain"),        ##########图例文字大小
        legend.position = 'right',               ############图例位置
        legend.key.size=unit(0.3,'cm'))             #############图例大小
print(p)
ggsave('DBN_predictioncase.tiff',  width=80, height=50, units="mm")


示图

R 绘制带有数字标签的多分类柱状图_第1张图片

你可能感兴趣的:(R实践)