更改堆叠条形图中条形图的顺序

时间:2021-02-23 12:11:48

标签: r ggplot2

谁能帮我改变条形图的顺序。这里的图例以正确的格式显示,但不知道为什么图表改变了

expected output

my output

ggplot(x,aes(x=Total,y=Type,fill=status))+
  geom_bar(stat='identity',width=0.2)+
  theme_classic()+
  #geom_text(aes(label = stat(x),group = Type),stat = "summary",fun = sum,vjust = .20,hjust= 1)+
  theme(axis.line.y = element_blank(),axis.ticks = element_blank(),legend.position = "bottom",
        axis.text.x = element_text(face = "bold", color = "black", size = 10, angle = 45, hjust = 1))+
  labs(x="", y="", fill="")+

 scale_fill_manual(values=c("#284a8d", "#00B5CE"))

1 个答案:

答案 0 :(得分:0)

这几乎是我所能得到的:

library(ggplot2)

ggplot(x,aes(x=Total,y=Type,fill=factor(status, c("Open", "Closed"))))+
  geom_bar(stat='identity',width=0.2)+
  geom_text(aes(label = after_stat(x)),
            stat = "summary",fun.data = function(x){data.frame(y = sum(x))}, 
            hjust= 0, position = position_stack(0))+
  geom_text(aes(label = after_stat(x), group = Type),
            stat = "summary",fun.data = function(x){data.frame(y = sum(x))}, 
            hjust= 0, position = position_stack(1))+
  labs(x="", y="", fill="")+
  scale_fill_manual(values=c("#284a8d", "#00B5CE"), 
                    limits = c("Closed", "Open")) +
  theme_classic() +
  theme(axis.line.y = element_blank(),
        axis.ticks = element_blank(),
        legend.position = "bottom",
        axis.text.x = element_text(face = "bold", color = "black", 
                                   size = 10, angle = 45, hjust = 1))

enter image description here