I'm trying to order the bars of my percent stacked barchart in R based on descending stack segment height. R automatically sorts my categorical data in alphabetical order (in both the barchart and its legend) but I'd like the data to be ordered so to have the biggest bars (the ones with the greatest stack segment height) on top of the barchart and the smallest at the bottom, in a descending manner. I don't know how to do this because I cannot manually set a specific order with a vector prior to using ggplot2: my dataset is quite big and I need it to be ordered based on total field area (a quantitative variable that changes for every single city I'm considering). Does anyone know hot to help me?
Asked
Active
Viewed 375 times
0
-
Look into `reorder`. Then reorder your categorical x axis variable by -[your quantative variable] – VvdL Aug 23 '22 at 11:59
2 Answers
0
You need to set your categorical variable as an ordered factor. For example, using the iris data, the default is for an alphabetical x-axis:
iris%>%
ggplot(aes(Species,Petal.Length))+
geom_col()
Using fct_reorder (from forecats, included in the tidyverse), you can change a character variable to a factor and give it an order in one step. Here I change the order of the x-axis such that is order by the average sepal width of the petal.
iris%>%
mutate(Species=fct_reorder(Species,Sepal.Width,mean))%>%
ggplot(aes(Species,Petal.Length))+
geom_col()

Justin Beresford
- 256
- 1
- 6
-
Thank you, it does seem to work this way! I do have a new issue though: my code for the ggplot is composed of a facet_wrap in order to get my dataframe as a small multiple sorted by region. When running the code now the first barchart (the one on the top left corner) is correctly sorted with the "tallest" stacked bars at the bottom and viceversa, while the other three barcharts seem to now have the bars sorted at random (not even in alphabetical order). Do you happen to know why and how could I fix this issue? Thank you in advance :) – Devon Aug 23 '22 at 16:17
-
Hard to say without seeing your data, but I think your solution might be fct_reorder2(). – Justin Beresford Aug 23 '22 at 17:00
-
Okay, thank you! I'll add an answer with my lines of code and some of the data as well as part of the graphs to make myself clear. – Devon Aug 24 '22 at 08:14
0
st_des_as %>%
mutate(COLTURA=fct_reorder(COLTURA,tot_area),.desc=F) %>%
ggplot(aes(x=" ", y=tot_area, fill=COLTURA)) +
geom_bar(position= "fill", stat="identity") +
facet_grid(~ZONA) +
labs(x=NULL, y="landcover (%)") +
scale_y_continuous(labels=function(x) paste0(x*100)) +
scale_fill_manual(name="CROP TYPE",values=colours_as) +
theme_classic() +
theme(legend.key.size = unit (10, "pt")) +
theme(legend.title = element_text(face="bold"))
geom_col()
here are some of my data, as you can see they are numerical values divided by region (ZONA) and crop type (COLTURA)
and here are the first graphs: the first one from the left is correctly sorted while the other three ones are sorted not following their bars' height but rather following the same sorting of the first graph, no matter the dimension of their own bars