1

I would like to have my stacked bar chart positioned along the x-axis based on total value. The default positioning is based on alphabetic order.

I used the following code to plot the stacked bar chart:

Country1 <- c(rep("Italy" , 3) , rep("Hungary" , 3) , rep("Belgium" , 3))
Shortage1 <- rep(c("Not relevant" , "Presumably active" , "Active") , 3)
Value1 <- c(11,823,480,1083,118,147,1,285,619)
DFshortage <- data.frame(Country1, Shortage1, Value1)
ggplot(DFshortage, aes(fill=Shortage1, y=Value1, x=Country1)) + 
  geom_bar(position="stack", stat="identity")

The output is as follows stacked bar chart

enter image description here

I would like to change the position of the bars based on total value, so the correct order should become Hungary, Italy, Belgium.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Max
  • 11
  • 2

2 Answers2

0

Try:

ggplot(DFshortage, aes(x=reorder(Country1, -Value1), y=Value1, fill=Shortage1)) +
  geom_bar(position="stack", stat="identity")
Magda D
  • 37
  • 5
0

There you go:

ggplot(DFshortage, aes(fill=Shortage1, y=Value1, x=reorder(Country1, -Value1))) +  geom_bar(position="stack", stat="identity")
CodingBiology
  • 262
  • 4
  • 13