How to control ordering of stacked bar chart using identity on ggplot2

Tom

Using this dummy data.frame

ts <- data.frame(x=1:3, y=c("blue", "white", "white"), z=c("one", "one", "two"))

I try and plot with category "blue" on top.

ggplot(ts, aes(z, x, fill=factor(y, levels=c("blue","white" )))) + geom_bar(stat = "identity")

enter image description here

gives me "white" on top. and

ggplot(ts, aes(z, x, fill=factor(y, levels=c("white", "blue")))) + geom_bar(stat = "identity")

enter image description here

reverses the colors, but still gives me "white" on top. How can I get "blue" on top?

Heroka

I've struggled with the same issue before. It appears that ggplot stacks the bars based on their appearance in the dataframe. So the solution to your problem is to sort your data by the fill factor in the reverse order you want it to appear in the legend: bottom item on top of the dataframe, and top item on bottom:

ggplot(ts[order(ts$y, decreasing = T),],
       aes(z, x, fill=factor(y, levels=c("blue","white" )))) + 
  geom_bar(stat = "identity")

enter image description here

Edit: More illustration

Using sample data, I created three plots with different orderings of the dataframe, I thought that more fill-variables would make things a bit clearer.

set.seed(123)
library(gridExtra)
df <- data.frame(x=rep(c(1,2),each=5),
                 fill_var=rep(LETTERS[1:5], 2),
                 y=1)
#original order   
p1 <- ggplot(df, aes(x=x,y=y,fill=fill_var))+
  geom_bar(stat="identity") + labs(title="Original dataframe")


#random order
p2 <- ggplot(df[sample(1:10),],aes(x=x,y=y,fill=fill_var))+
  geom_bar(stat="identity") + labs(title="Random order")
#legend checks out, sequence wird

#reverse order
p3 <- ggplot(df[order(df$fill_var,decreasing=T),],
             aes(x=x,y=y,fill=fill_var))+
  geom_bar(stat="identity") + labs(title="Reverse sort by fill")

plots <- list(p1,p2,p3)

do.call(grid.arrange,plots)

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Stacked Bar Chart in R using ggplot2 (not possible in excel)

Set border in a stacked bar chart using ggplot2

How to modify the limits of y axis of a stacked bar chart in polar coordinates using ggplot2?

how to change the stacked bar chart using ggplot2 (percentage, sort) in R

how to make single stacked bar chart in ggplot2

How to created a grouped and stacked bar chart in R with ggplot2

How modify stacked bar chart in ggplot2 so it is diverging

Divergent stacked bar chart with ggplot2: issue with factor ordering in legend

ggplot2 stacked bar chart: each bar is one colour, stacked groups are shown using alpha

Advanced stacked bar chart ggplot2

How to set ordering of categories in Pandas stacked bar chart

How to show value label in stacked and grouped bar chart using ggplot

How to reorder the stacked segments of a bar chart using ggplot

Ordering Data with Stacked Bar Charts in ggplot2

How to create stacked bar chart using react-chartjs-2?

Ordering of elements in Pandas stacked bar chart

How to plot a stacked bar chart using hvplot?

R Create stacked bar chart from non-numerical data using ggplot2

Create a stacked bar chart with already grouped data using ggplot2

How to plot a Stacked and grouped bar chart in ggplot?

How to create a stacked bar chart in r with ggplot

How to organize percentage values on top of a stacked bar chart ggplot2

How do I reorder a stacked bar chart in ggplot2 by the value of one portion of the stack?

How to arrange y axis in bar chart using ggplot2

How to display 0 value in a bar chart using ggplot2

How to change the position of stacked stacked bar chart in ggplot in R?

Ordering Legend in Multi-Bar ggplot2 Chart

How to control the spacings of the x Axis label positions in a stacked bar chart?

how to control the order of groups in a vega-lite stacked bar chart