I am trying to make a stacked bar chart and I would like to reorder the variables on the x-axis based on the data from a single category. In the example below there are three x values each of which have values corresponding to three categories. How would it be possible to plot the graph while sorting the name values for increasing abundance of "bb".
Although this question is similar to other questions about reordering categorical variables the difference here is that the ordering is based on a subset of one column's data. Any suggestions appreciated.
#create the dataframe
name = c('a', 'a', 'a', 'b', 'b', 'b','c','c','c')
cat = c("aa", "bb", "cc", "aa", "bb", "cc","aa", "bb", "cc")
percent = c( 5 , 5, 90, 40, 40 , 20, 90,5,5)
df = data.frame(name, cat, percent)
#stacked barchart with default ordering
ggplot(df, aes(x=name,y=percent, fill=cat)) + geom_bar(position="fill")
#I'm looking to reorder the x-axis by the `percent` values for category "bb"
vals = df[ df$cat == 'bb', ] #subset
xvals = vals[with(vals, order(percent)), ]$name #get values
ggplot(df, aes(x =reorder(name, xvals ), y = percent, fill=cat])) + geom_bar(position="fill") #order with new values
like this...?
There are two problems here. The first is that you want to resort based on the percent in
bb
. The second is thaggplot
always sorts a categorical x-axis alphabetically, so you need to get around that.First, to resort your data, ironically you need to transform to wide format, sort, and then re-transform to long format:
Then:
Produces this: