2

I have the following command that I would like to draw a histogram in an ordered manner.

So the code is as follows:

ggplot(upstream, aes(x=type, y=round(..count../sum(..count..) * 100, 2))) + geom_histogram(fill= "red", color = "red") + xlab ("Vehicle Type") +
    ylab("Percentage of Vehicles in the Category (%)") + ggtitle ("Percentage of Upstream Vehicles by Type") +
    stat_bin(geom="text", aes(label=round(..count../sum(..count..) * 100, 2)), vjust=-0.5)

The output is:

I would like to arrange the bars in an ordered manner, so I use reorder() function in aes, but this gives me the following problem:

 stat_bin requires the following missing aesthetics x

How can I use reorder without getting this error? I couldn't seem to be able to figure it out with the posted solutions.

Thanks for suggestions in advance.

EDIT 1: I fixed what I was looking for based on joran's suggestion with geom_bar() as follows in case anyone needs it:

# Reorder the factor you are trying to plot on the x-side (descending manner)
upstream$type <- with(upstream, reorder(type, type, function(x) -length(x)))
# Plotting
ggplot(upstream, aes(x=type, y=round(..count../sum(..count..) * 100, 2))) + geom_bar(fill= "blue", color = "blue") + xlab ("Vehicle Type") +
    ylab("Percentage of Vehicles in the Category (%)") + ggtitle ("Percentage of Upstream Vehicles by Type") +
    stat_bin(geom="text", aes(label=round(..count../sum(..count..) * 100, 2)), vjust=-0.5)
krlmlr
  • 25,056
  • 14
  • 120
  • 217
kukushkin
  • 312
  • 4
  • 16
  • just guessing but hard to tell without seeing your actual data: is type a factor-variable? if so you would probably need to convert to numeric beforehand to be able to reorder... – grrgrrbla Apr 14 '15 at 17:20
  • 1
    Please post sample input data do make this problem [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – MrFlick Apr 14 '15 at 17:26
  • What if you recalculate all the values and set the order of levels in `type` to correspond with the values for `y`? – Roman Luštrik Apr 14 '15 at 17:26
  • 1
    If you're reordering the bars, then it isn't really a histogram, is it? Maybe this would be simpler with `geom_bar`. – joran Apr 14 '15 at 17:45
  • @grrgrrbla Yes, it is a factor. I will try to convert it to numeric and reorder the data again. Thanks for the tip. – kukushkin Apr 14 '15 at 18:46
  • @Roman I of course can do that but I thought ggplot2 is powerful enough to do this kind of operation immediately while drawing rather than needing previous calculation. – kukushkin Apr 14 '15 at 18:51
  • @Joran yes you're right! it's a barplot.I am always confused with both. Thanks for correcting me, appreciate that! With geom_bar, I am having the same problem, though. – kukushkin Apr 14 '15 at 18:51
  • Thank you very much @joran I updated the code as follows based on your geom_bar() suggestion and fixed the situation. – kukushkin Apr 14 '15 at 19:24
  • When Mohammed won't come to the mountain, the mountain must come to Mohammed... if you catch my drift. :) – Roman Luštrik Apr 15 '15 at 07:11

1 Answers1

2

Here is a reproducible example of the behaviour you are looking for. It is copied from FAQ: How to order the (factor) variables in ggplot2

# sample data.
d <- data.frame(Team1=c("Cowboys", "Giants", "Eagles", "Redskins"), Win=c(20, 13, 9, 12))

# basic layer and options
p <- ggplot(d, aes(y=Win))

# default plot (left panel)
# the variables are alphabetically reordered.
p + geom_bar(aes(x=Team1), stat="identity")

# re-order the levels in the order of appearance in the data.frame
d$Team2 <- factor(d$Team1, as.character(d$Team1))
# plot on the re-ordered variables (Team2) 
p + geom_bar(aes(x=Team2), data=d, stat="identity") 
Art
  • 1,165
  • 6
  • 18