I don't use ggplot2
, hence my explanation is likely to be wrong. I'll request you to verify it, and maybe you or someone else can rectify my mistakes.
My idea is that stat = 'identity'
has nothing to do with the order. I used geom_col
, and you used geom_bar(stat = 'identity')
, and these two are equivalent.
What's wrong with your code is that the x
values are plotted in the increasing order, which is by default the alphabetical order. You want to change that using SO2
, and hence you'll have to reorder them using that. I think reorder
changes the ordering of the levels of the x
argument according to the corresponding values of the X
argument. Here, thus Entity
is reordered according to mean value of SO2
and their updated ordering is with respect to the SO2
values, not with respect to the alphabetic order. Hence, plotting with these reordered Entity
leads to the result you desire.