简体   繁体   中英

R boxplot : How do I "reorder" the date field?

The table consist of a Month/Year field ie "January 2016". How do I use the reorder a boxplot to display the X axis in date order (Jan 2016...Feb 2016.... What I tried using the following code :

     boxplot(YR$S~reorder(format(YR$MY,'%M %Y'),YR$MY),outline =FALSE)
<pre>

IDX  MY Day V Time G  S   W
24 January 2015   1 G 1821 6 11  71
25 January 2015   2 G 1600 9 15 1
26 January 2015   5 G 1700 5 14  64
27 January 2015   6 F 1805 3 14  4
28 January 2015   7 G 1716 3 15  45
29 January 2015   9 F 1910 3  8  38

Convert your dates to class Date , so that boxplot can pick the appropriate continuous scale for the x axis and order your values automatically:

y <- YR$S
oldloc <- Sys.getlocale("LC_TIME"); Sys.setlocale("LC_TIME", "english")
x <- as.Date(with(YR, paste(paste(MY, Day, sep = "-"))), format="%B-%Y-%d")
Sys.setlocale("LC_TIME", oldloc)
boxplot(y~x)

I set the locale to English so that R knows how to interpret "January" in foreign languages (in German for example it's "Januar"). You may omit that, if you are set to English already...

Data used:

YR <- read.table(header=T, text="
 MY Day V Time G  S   W
February-2015   1 G 1821 6 11  71
January-2015   2 G 1600 9 15 1
January-2015   5 G 1700 5 14  64
January-2015   6 F 1805 3 14  4
January-2015   7 G 1716 3 15  45
January-2015   9 F 1910 3  8  38")

As I mentioned above depending on the format of the data and how best to bin the data (ie monthly daily) would affect the recommendation. Below is different approaches which I would consider (may not be the best way but it can get the job done):

#Sample data
string<-rep(c("January 2016", "February 2016", "March 2016"), 3)
day<-rep(c(1:3), each=3)
value<-runif(9,10, 20)
#data frame with string, int and float
df<-data.frame(string, day, value)

#Date as string
boxplot(df$value~df$string, las=2, main="String")
#undersirable - x - axis not in order

#Date as a Date Class
#convert to Date Class 
#xdate<-as.Date(paste(df$string, day), format= "%B %Y %d")
#Need to convert everything to first of month to bin by month
xdate<-as.Date(paste(df$string, 1), format= "%B %Y %d")
b<-boxplot(df$value~xdate, las=2, main="Date", names=unique(months(xdate)))
#Good - may need work on x axis labels

#Date as a factor
#convert to factor
xfactor<-as.factor(df$string)
#sets the factors in month order (drops the year suffix)
xfactor<-factor(xfactor, levels = paste(month.name, "2016"))
#remove unused levels
xfactor<-droplevels(xfactor)
boxplot(df$value~xfactor, las=2, main="factor")
#Good - may need work on x axis labels depending in timeframe on interest

All three attempts have their pro and cons and depending on the initial format, how much data, report frequency and the final results determines the best approach. Hope this helps.

Thanks one and all for your responses. Turns out that there is a much shorter and simpler soution. The library "Rlab" has a built in box chart + binning function called "bplot. Here's a copde example: (MY = month-year field, S = number of sunspots)

 library(Rlab)
 bplot(MY,S)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM