I must be doing something stupid, but reorder in the aes() portion of ggplot periodically but inexplicably doesn't work for me. Here are two versions of MWE that reproduce the problem on my system. Neither one reorders the letters by the total. The plot should show b, c, a on the X axis.
(df <- data.frame(letters = c("a", "b", "c"), nums = 1:3, total = c("150", "50", "100")))
reorder.plot <- ggplot(df, aes(reorder(letters, total), y = nums)) +
geom_point()
reorder.plot
reorder.plot.stats <- ggplot(df, aes(stats::reorder(letters, total), y = nums)) +
geom_point()
reorder.plot.stats
As can be seen, in the second example I specified the stats package.
One post said to reorder the data frame before calling ggplot, but I know reorder has worked in the past many times for me reorder data in ggplot Another question created a new, ordered variable and used that variable in ggplot. Understanding how "reorder" in R works
I am running under Windows 8 and have these packages loaded. If this is relevant, how can I tell if reorder() is being masked, even when I specify stats::?
search() 1 ".GlobalEnv" "package:extrafont"
"package:RCurl" "package:bitops" [5] "package:qdap" "package:qdapTools"
"package:qdapDictionaries" "package:XML" [9] "package:ReporteRs" "package:ReporteRsjars"
"package:lubridate" "package:gridExtra" [13] "package:RColorBrewer" "package:dplyr" "package:scales" "package:Hmisc" [17] "package:Formula"
"package:survival" "package:splines"
"package:lattice" [21] "package:grid"
"package:stringr" "package:XLConnect"
"package:reshape2" [25] "package:plyr"
"package:ggplot2" "tools:rstudio" "package:stats" [29] "package:graphics" "package:grDevices"
"package:utils" "package:datasets" [33] "package:methods" "Autoloads" "package:base"
Thank you, and I am already expressing my regret at not understanding the obvious.
The total column is a factor, but reorder() takes numerics or logicals. If you remove the quotes around 150, 50, 100 when you specify the total values, or if you convert total to a numeric first with as.numeric(), it will work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With