r - Ordering of bars in ggplot -
i have looked through answers in forum cannot seem find answer specific problem. have following data , want create bar chart bars ordered largest smallest in terms of "value", rather having them in alphabetical order:
breadth_data <- read.table(textconnection("stakeholder value 'grantseekers' 0.90 'donors' 0.89 'community' 0.55 'hurricane relief fund' 0.24 'media' 0.19 'employment seekers' 0.12 'affiliates' 0.10 'youth' 0.09 'women' 0.02 'former board members' 0.01"), header=true)
then basic bar chart:
c <- ggplot(breadth_data, aes(x=stakeholder, y=value)) c + geom_bar(stat="identity") + coord_flip() + scale_y_continuous('') + scale_x_discrete('')
i have tried many of different reorderings , transformations i've seen on stackoverflow cannot seem find 1 works. sure simple, appreciate help!
thanks,
greg
you want function reorder()
:
breadth_data <- transform(breadth_data, stakeholder = reorder(stakeholder, value))
which gives:
if want them other way round, easy way use order()
on value
inside reorder()
call:
breadth_data <- transform(breadth_data, stakeholder = reorder(stakeholder, order(value, decreasing = true)))
Comments
Post a Comment