R - reordering histogram bars - ggplot2 -


i have following command draw histogram in ordered manner.

so code 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 category (%)") + ggtitle ("percentage of upstream vehicles type") +     stat_bin(geom="text", aes(label=round(..count../sum(..count..) * 100, 2)), vjust=-0.5) 

the output is:

i arrange bars in ordered manner, use reorder() function in aes, gives me following problem:

 stat_bin requires following missing aesthetics x 

how can use reorder without getting error? couldn't seem able figure out posted solutions.

thanks suggestions in advance.

edit 1: fixed looking based on joran's suggestion geom_bar() follows in case needs it:

# reorder factor trying plot on 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 category (%)") + ggtitle ("percentage of upstream vehicles type") +     stat_bin(geom="text", aes(label=round(..count../sum(..count..) * 100, 2)), vjust=-0.5) 

here reproducible example of behaviour looking for. copied faq: how order (factor) variables in ggplot2

# sample data. d <- data.frame(team1=c("cowboys", "giants", "eagles", "redskins"), win=c(20, 13, 9, 12))  # basic layer , options p <- ggplot(d, aes(y=win))  # default plot (left panel) # variables alphabetically reordered. p + geom_bar(aes(x=team1), stat="identity")  # re-order levels in order of appearance in data.frame d$team2 <- factor(d$team1, as.character(d$team1)) # plot on re-ordered variables (team2)  p + geom_bar(aes(x=team2), data=d, stat="identity")  

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -