Hughes, Ed
2010-Sep-27 19:59 UTC
[R] Alphabetical sequence of data along the x-axis in a box plot
Hello All, I noticed when I generated some boxplots, the data is presented in alphabetical order along the x-axis (the data in this case was the four quandrants of a sample area (NE,NW, SE, SW) that was my first column of data). Is there a way to have R plot the data in a different order? I imagine you could use a dummy variable, but didn't know if there might be a simple argument that will address this? Thanks for any guidance, Eddie Hughes [[alternative HTML version deleted]]
Joshua Wiley
2010-Sep-27 20:16 UTC
[R] Alphabetical sequence of data along the x-axis in a box plot
Hi Eddie, I've been on a role with the iris data, so I figure why stop. Assuming that one variable is a factor, you can easily reverse it, and if you want fine tuned control, then just reorder the levels. Here is an example: dat <- iris boxplot(Sepal.Length ~ Species, data = dat) boxplot(Sepal.Length ~ rev(Species), data = dat) # They had been ordered alphabetically, now I am changing them dat$Species <- factor(dat$Species, levels = c("versicolor", "virginica", "setosa"), labels = c("versicolor", "virginica", "setosa")) boxplot(Sepal.Length ~ Species, data = dat) Cheers, Josh On Mon, Sep 27, 2010 at 12:59 PM, Hughes, Ed <ehughes at conshelf.com> wrote:> Hello All, > > > > I noticed when I generated some boxplots, the data is presented in > alphabetical order along the x-axis (the data in this case was the four > quandrants of a sample area (NE,NW, SE, SW) that was my first column of > data). ?Is there a way to have R plot the data in a different order? ?I > imagine you could use a dummy variable, but didn't know if there might > be a simple argument ?that will address this? > > > > Thanks for any guidance, > > Eddie Hughes > > > > > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
Peter Alspach
2010-Sep-27 20:50 UTC
[R] Alphabetical sequence of data along the x-axis in a box plot
Tena koe Eddie One way: eddie <- data.frame(grp=rep(c('small','medium','large','very large'), each=20), wgt=rnorm(80, 100, 10)) with(eddie, plot(grp, wgt)) eddie$grp <- factor(eddie$grp, levels=c('small','medium','large','very large')) with(eddie, plot(grp, wgt)) HTH ... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Hughes, Ed > Sent: Tuesday, 28 September 2010 9:00 a.m. > To: r-help at r-project.org > Subject: [R] Alphabetical sequence of data along the x-axis in a box > plot > > Hello All, > > > > I noticed when I generated some boxplots, the data is presented in > alphabetical order along the x-axis (the data in this case was the four > quandrants of a sample area (NE,NW, SE, SW) that was my first column of > data). Is there a way to have R plot the data in a different order? I > imagine you could use a dummy variable, but didn't know if there might > be a simple argument that will address this? > > > > Thanks for any guidance, > > Eddie Hughes > > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.The contents of this e-mail are confidential and may be subject to legal privilege. If you are not the intended recipient you must not use, disseminate, distribute or reproduce all or any part of this e-mail or attachments. If you have received this e-mail in error, please notify the sender and delete all material pertaining to this e-mail. Any opinion or views expressed in this e-mail are those of the individual sender and may not represent those of The New Zealand Institute for Plant and Food Research Limited.
Dan_K
2011-Oct-13 15:28 UTC
[R] Alphabetical sequence of data along the x-axis in a box plot
The easiest work-around I've found for this problem is to create a vector in your data frame just using numbers to order them how you want, create a separate "labeling" data frame with those numbers and corresponding text labels, and then enter the vector with the grouping names from the labeling frame in boxplot's 'names' argument. As such: dir <- "D:\\" setwd(dir) data <- read.csv("ODR.csv") type <- read.csv("soil_type.csv") boxplot(data=data, Sat..ODR~Type_num, *names=type$Soil.type*, col="light green", main="ODR by soil type", xlab="Soil type", ylab="ODR") The labeling data frame should look something like this:> data.frame(type)Type_num Soil.type 1 1 DCL 2 2 UCL 3 3 Sand Hope this helps! -- View this message in context: http://r.789695.n4.nabble.com/Alphabetical-sequence-of-data-along-the-x-axis-in-a-box-plot-tp2716131p3901884.html Sent from the R help mailing list archive at Nabble.com.