Sorting boxplots by median in ggplot2

13,910 views
Skip to first unread message

Nicolay Cunha

unread,
Jul 11, 2013, 10:19:39 PM7/11/13
to ggp...@googlegroups.com
# Dear all,
# I have a question regarding the ordering of boxplots in relation to the
# median in a graph of three factors.
# I want the boxplots getting ordered from the highest to the lowest median
#(stair), within each level of the factor that gives the colors in the
# graphic
# Below the a minimum code


################################################################################################################################
#install.packages("ggplot2")
library(ggplot2)
cmr <- read.table ('http://pastebin.com/raw.php?i=61J9W4Gt', header = T)
str(cmr)

#ordering factors
cmr$height <- factor(cmr$height, levels = c('h','m','l'))
cmr$Pulse <- factor (cmr$Pulse, levels = c("1LF_Low_Short",
"2PF_Low_Short", "2PF_Medium_Medium",
"3GF_Medium_Short","3GF_Medium_Long"))

#plot
ggplot(cmr, aes( height , variable,
colour = factor(pop, levels = pop[order(factor(Pulse))]),
fill = factor(Pulse)))+
xlab ('Anther levels')+
ylab ('Length (mm)')+
scale_colour_manual( values = rep("black", 24), legend = FALSE) +
scale_fill_manual(name = 'Flood Pulse intensity', values =
c("#E5E5E5", "#ACACAC", "#747474", "#3B3B3B", "#030303"))+
geom_boxplot(mapping = NULL, data = NULL, stat = "boxplot", position =
"dodge", outlier.colour = "black",
outlier.shape = 8, outlier.size = 1.5, notch = FALSE, notchwidth =
0.5, show_guide = TRUE)+
theme_bw()
##################################################################

# The question is, how to order the boxplots within each level of Flood
# in descending order of median (several stairways)?
# []

Greg Snow

unread,
Jul 12, 2013, 11:21:29 AM7/12/13
to Nicolay Cunha, ggplot2
R generally treats information like the ordering of factor levels as a property of the data rather than as a property of the graph.  This means that if you set the order of the factor levels in the factor itself (see the reorder function) then ggplot2 and other plotting and tabling functions will honor that ordering.  Since this should be a property of the data there is generally not an option in the plotting function to change the order.


--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2
 
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Gregory (Greg) L. Snow Ph.D.
538...@gmail.com

Winston Chang

unread,
Jul 19, 2013, 12:42:23 PM7/19/13
to Nicolay Cunha, ggplot2
You can use reorder() to change the order of factor levels based on another variable. You can modify the original data, or you can put it in line so that ggplot calculates it:

# Original unordered version
ggplot(InsectSprays, aes(x = spray, y = count)) + geom_boxplot()

# Ordered by median
ggplot(InsectSprays, aes(x = reorder(spray, count, FUN=median), y = count)) + geom_boxplot()

-Winston



--
Reply all
Reply to author
Forward
0 new messages