stat_summary 函数改变图例的顺序

时间:2021-06-27 11:48:40

标签: r ggplot2

使用 stat_summary 函数获取每个条形图的总和并将其绘制到堆叠条形图上。 它工作正常,但不知何故扰乱了右侧的“老化”表。它不是有序的。 当我删除 stat_summary 代码行时,它会按顺序显示我。但我想显示每个条形的总数;因此,我使用了 stat_summary 函数。

df3 <- group_by(df, AgeGroup, Org_Name)
totalbarsum <- aggregate(df3$ClaimValue_INR, by=list(Category=df3$Org_Name), FUN=sum) 
dfsecondgraph <- totalsumorg
valueorg <- format(dfsecondgraph$x,big.mark=",",scientific=FALSE)

dfsecondgraph$Aging <- dfsecondgraph$Group.2
datareordersecond <- dfsecondgraph

ggplot(datareordersecond,aes(x = reorder(Category, x,FUN = sum), y = x, fill = Aging))+
  geom_bar(stat = "summary", fun = sum, position = position_stack())+
  geom_col(position = position_stack(reverse = TRUE))+
  geom_text(size = 3, position = position_stack(vjust = 0.5,reverse = TRUE), check_overlap = T,label = valueorg) +
  stat_summary(fun = sum, aes(label = scales::comma (..y..), group = Category),size = 4,vjust = -1, hjust = 0,fontface = "bold",geom = "text")+
  xlab("PAN India Dealerships ") +
  ylab("Claim Value INR") +
  ggtitle("Pending Claims Status : PAN India Dealerships ")+theme(plot.title = element_text(lineheight=3, face="bold", color="black", size=20))+
  scale_y_continuous(labels = comma)+
  theme(axis.text.x = element_text(angle = 0, hjust = 1, size=15))+
  theme(axis.text.y = element_text(angle = 0, hjust = 1, size=15))+
  theme(axis.title.y = element_text(size = rel(1.8), angle = 90))+
  theme(axis.title.x = element_text(size = rel(1.8), angle = 00))+
  scale_fill_manual(values = c("#F8766D", "#7CAE00", "#00BFC4","#C77CFF","#F0E442","#f37735"))+
  coord_flip()

enter image description here

0 个答案:

没有答案