R 中的堆叠条形图:重新排序数据

Stacked bar graph in R: re-ordering the data

我制作了一个条形图,但是我想要切换数据。 我想要底部的蓝色区域(活细胞)和顶部的死细胞。 我试过重新排序,通过将其作为一个因素重新排序,但都没有用。

谁能帮帮我?

我现在的代码:

b_stack1<-ggplot(Stack1, aes(x=Condition, y=N, fill=Type)) + 
  geom_bar(aes(fill = Type), stat="identity", position="stack", color="black") +
  scale_fill_manual(values=c("lightblue","grey")) +
  theme_bw() + theme(legend.position="right", axis.title.x = element_blank())

我现在的图表:

library(forcats)

b_stack1<-ggplot(Stack1, aes(x=Condition, y=N, fill=fct_shift(Type))) + 
  geom_bar(aes(fill = Type), stat="identity", position="stack", color="black") +
  scale_fill_manual(values=c("lightblue","grey")) +
  theme_bw() + theme(legend.position="right", axis.title.x = element_blank())

您尝试过position_stack(reverse = TRUE)吗?

b_stack1<-ggplot(Stack1, aes(x=Condition, y=N, fill=Type)) + 
  geom_bar(aes(fill = Type), stat="identity", position = position_stack(reverse = TRUE)), color="black") +
  scale_fill_manual(values=c("lightblue","grey")) +
  theme_bw() + theme(legend.position="right", axis.title.x = element_blank())
    `install.packages("forcats") 
b_stack1<-ggplot(Stack1, aes(x=Condition, y=N, fill = forcats::fct_rev(Type))) + 

geom_bar(stat="identity", position="stack", color="black") + scale_fill_manual(值=c("淡蓝色","灰色"))`

Forcats 成功了