KLB
KLB

Reputation: 97

Changing Order of Stacked Geom_Bar

I'm finalizing a graph and hoping to change the order of two segments of my stacked bar graph.

Current Stacked Bar Chart

I have tried both the forcats::fct_rev() solution as offered in documentation and Reverse stacked bar order as well as position_fill(reverse = TRUE) in my geom_bar argument.

Simplified code:
plot2<-ggplot(r_4,aes(x=V1,y=V3,fill=V2))+
  coord_flip()+
  geom_bar(stat="identity")
Data:
structure(list(V1 = structure(c(1L, 1L), .Label = "Reagan", class = "factor"), 
    V2 = structure(1:2, .Label = c("Case-Based", "Term-Based"
    ), class = "factor"), V3 = structure(c(2L, 1L), .Label = c("0.36", 
    "0.64"), class = "factor")), .Names = c("V1", "V2", "V3"), row.names = c(NA, 
-2L), class = "data.frame")

I would like the smaller piece ("Term-Based") on the left and the larger piece ("Case-Based") on the right.

Thank you!

Upvotes: 2

Views: 344

Answers (1)

heds1
heds1

Reputation: 3438

edit: change V3, not V2

As akrun commented, change factor levels, then it should work:

r_4$V3 <- factor(c("0.36", "0.64"))

plot2<-ggplot(r_4,aes(x=V1,y=V3,fill=V2))+
  coord_flip()+
  geom_bar(stat="identity")

Upvotes: 1

Related Questions