I would like to show boxplots for multiple variables, and rank them by column means in descending order, just like in the Performance Analytics package. I use the following code to generate the boxplots:
zx <- replicate (5, rnorm(50))
zx_means <- (colMeans(zx, na.rm = TRUE))
boxplot(zx, horizontal = FALSE, outline = FALSE)
points(zx_means, pch = 22, col = "darkgrey", lwd = 7)
So far I have not been able to come up with a way to rank them as described above. I have tried using both sort and order, but without any satisfying results so far.
Any help would be much appreciated.
order works fine for me!?:
colnames (zx) <- seq_len (ncol (zx))
boxplot(zx [, order (zx_means)], horizontal = FALSE, outline = FALSE)
points(zx_means [ order (zx_means)], pch = 22, col = "darkgrey", lwd = 7)
Using ggplot2 this gets the job done using your example data:
library(ggplot2)
library(reshape)
zx <- replicate (5, rnorm(50))
# ggplot2 uses long-shaped data.frame's, not matrices
zx_flat = melt(zx)[c(2,3)]
names(zx_flat) = c("cat","value")
# Here I calculate the mean per category
zx_flat = ddply(zx_flat, .(cat), mutate, mn = mean(value))
zx_flat = sort_df(zx_flat, "mn") # Order according to mean
# Here I manually set the order of the levels
# as this is the order ggplot2 uses
zx_flat$cat = factor(zx_flat$cat, levels = unique(zx_flat$mn))
# make the plot
ggplot(aes(factor(mn), value), data = zx_flat) + geom_boxplot()
and we get:
来源:https://stackoverflow.com/questions/9871106/boxplots-ranked-by-mean-value