philipharrison
2011-Jun-24 16:52 UTC
[R] ggplot2 month and year boxplot x axis order problem
Hi
I am very new to R, I am attempting to produce a monthly boxplot with the
following fish thermal telemetry data:
ID Temp Date.Time Month.Year Month Week Shortdate
1 1734 4.4140 04/05/2010 11:56 05,2010 May 19 04/05/2010
2 1734 4.1002 04/05/2010 12:06 05,2010 May 19 04/05/2010
3 1734 3.9433 04/05/2010 12:09 05,2010 May 19 04/05/2010
4 1734 3.6295 04/05/2010 12:29 05,2010 May 19 04/05/2010
5 1734 3.6295 04/05/2010 12:34 05,2010 May 19 04/05/2010
6 1734 3.6295 04/05/2010 12:37 05,2010 May 19 04/05/2010
I constructed my data in access. I have data from May2010 to april 2011
When I use ggplot2
plot6<-ggplot(data=Good,aes(Month.Year,Temp))> + geom_boxplot()
the x axis factor order is jan 2011,feb2011, march 2011, april2011, May
2010etc. I would like the months to be in chronological order
I have tried a number of conversions using format and as.date and as.POSIXlt
with no luck including
Goodyearmonth<-as.Date(Good$Shortdate,format="%Y/%m")
and i tried
Good$Gmonth<- with(Good,factor(as.POSIXlt(Shortdate)$mon)
If I pivot my data by month in access so it looks like this
May-10 Jun-10 Jul-10
4.1002 7.3951 8.3365
3.9433 7.3951 8.1796
3.6295 7.3951 8.0227
then a simple boxplot() works however I would like to use the ggplot2
package because eventually I would like to change the whiskers to 95% and 5%
quantiles (I think I know how to do this
http://r.789695.n4.nabble.com/Custom-Plot-means-SD-amp-5th-95th-Plotmeans-or-Boxplot-td848126.html)
Thanks for any help you can provide
Philip Harrison
PhD Student Fisheries ecology
Uwaterloo
--
View this message in context:
http://r.789695.n4.nabble.com/ggplot2-month-and-year-boxplot-x-axis-order-problem-tp3623029p3623029.html
Sent from the R help mailing list archive at Nabble.com.
stephen sefick
2011-Jun-24 17:57 UTC
[R] ggplot2 month and year boxplot x axis order problem
This needs to be made reproducible. In other words it would be helpful if you would use dput to share the data. dput(Good) copy the output to the email in this format x <- () with the output from dput inside the () then write the code for ggplot under this line so that we can then just modify things to make it work. On Fri, Jun 24, 2011 at 11:52 AM, philipharrison <pharriso at uwaterloo.ca> wrote:> Hi > > I am very new to R, I am attempting to produce a monthly boxplot with the > following fish thermal telemetry data: > > ? ?ID ? Temp ? ? ? ?Date.Time Month.Year Month Week ?Shortdate > 1 1734 4.4140 04/05/2010 11:56 ? ?05,2010 ? May ? 19 04/05/2010 > 2 1734 4.1002 04/05/2010 12:06 ? ?05,2010 ? May ? 19 04/05/2010 > 3 1734 3.9433 04/05/2010 12:09 ? ?05,2010 ? May ? 19 04/05/2010 > 4 1734 3.6295 04/05/2010 12:29 ? ?05,2010 ? May ? 19 04/05/2010 > 5 1734 3.6295 04/05/2010 12:34 ? ?05,2010 ? May ? 19 04/05/2010 > 6 1734 3.6295 04/05/2010 12:37 ? ?05,2010 ? May ? 19 04/05/2010 > > I constructed my data in access. I have data from May2010 to april 2011 > > When I use ggplot2 > > plot6<-ggplot(data=Good,aes(Month.Year,Temp)) >> + geom_boxplot() > > the x axis factor order is jan 2011,feb2011, march 2011, april2011, May > 2010etc. I would like the months to be in chronological order > > I have tried a number of conversions using format and as.date and as.POSIXlt > with no luck including > > Goodyearmonth<-as.Date(Good$Shortdate,format="%Y/%m") > > and i tried > > Good$Gmonth<- with(Good,factor(as.POSIXlt(Shortdate)$mon) > > If I pivot my data by month in access so it looks like this > May-10 ?Jun-10 ?Jul-10 > 4.1002 ?7.3951 ?8.3365 > 3.9433 ?7.3951 ?8.1796 > 3.6295 ?7.3951 ?8.0227 > > then a simple boxplot() works however I would like to use the ggplot2 > package because eventually I would like to change the whiskers to 95% and 5% > quantiles (I think I know how to do this > http://r.789695.n4.nabble.com/Custom-Plot-means-SD-amp-5th-95th-Plotmeans-or-Boxplot-td848126.html) > > Thanks for any help you can provide > > Philip Harrison > PhD Student Fisheries ecology > Uwaterloo > > -- > View this message in context: http://r.789695.n4.nabble.com/ggplot2-month-and-year-boxplot-x-axis-order-problem-tp3623029p3623029.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Stephen Sefick ____________________________________ | Auburn University? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? | | Biological Sciences ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ?? ? ? ?| | 331 Funchess Hall? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| | Auburn, Alabama? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ??? | | 36849? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? | |___________________________________| | sas0025 at auburn.edu? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ?| | http://www.auburn.edu/~sas0025? ? ? ? ? ?? ? ? | |___________________________________| Let's not spend our time and resources thinking about things that are so little or so large that all they really do for us is puff us up and make us feel like gods.? We are mammals, and have not exhausted the annoying little problems of being mammals. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? -K. Mullis "A big computer, a complex algorithm and a long time does not equal science." ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? -Robert Gentleman
philipharrison
2011-Jun-24 18:15 UTC
[R] ggplot2 month and year boxplot x axis order problem
Hi,
Thanks for your speedy reply! The file is pretty big (I have 18,000
datapoints), there is not enough room on the r screen for the entire output
so I have just cut out a few lines from the end of the dput . I dont know if
that helps?
X <-(12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L), .Label = c("01,2011",
"02,2011",
"03,2011", "04,2011", "05,2010",
"06,2010", "07,2010", "08,2010",
"09,2010", "10,2010", "11,2010",
"12,2010"), class = "factor")), .Names
= c("ID",
"Temp", "Date.Time", "Month.Year",
"Month", "Week", "Shortdate",
"MY"), row.names = c(NA, -18631L), class = "data.frame"))
my ggplot code I have tried is
Plot1<-ggplot(data=Great,aes(Great$Month.Year,Great$Temp))+
geom_boxplot()
I dont know if that makes it any clearer
Phil
--
View this message in context:
http://r.789695.n4.nabble.com/ggplot2-month-and-year-boxplot-x-axis-order-problem-tp3623029p3623230.html
Sent from the R help mailing list archive at Nabble.com.
philipharrison
2011-Jun-24 19:07 UTC
[R] ggplot2 month and year boxplot x axis order problem
Hi thanks for your patience, I understand what you mean now. I made some
dummy data;
x<-(structure(list(ID = c(1734L, 1734L, 1734L, 1734L, 1734L, 1734L,
1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L,
1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L,
1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L,
1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L,
1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L,
1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L,
1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L,
1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L,
1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L,
1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L,
1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L,
1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L,
1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L,
1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L,
1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L,
1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L,
1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L,
1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L, 1734L,
1734L, 1734L, 1734L, 1734L), Temp = c(4.414, 4.1002, 3.9433,
4.414, 4.414, 4.414, 4.414, 4.414, 4.414, 4.414, 4.414, 4.414,
4.414, 4.414, 4.414, 4.414, 4.414, 4.414, 4.414, 4.414, 4.414,
4.414, 3.9433, 3.9433, 3.9433, 3.9433, 3.9433, 3.9433, 3.9433,
3.9433, 3.9433, 3.9433, 3.9433, 3.9433, 3.9433, 3.9433, 3.9433,
3.9433, 3.9433, 3.9433, 3.9433, 3.9433, 3.9433, 3.9433, 3.9433,
11.6314, 11.6314, 11.6314, 11.6314, 11.6314, 11.6314, 11.6314,
11.6314, 11.6314, 11.4745, 11.4745, 11.4745, 11.4745, 11.6314,
11.6314, 11.7883, 12.1021, 12.5728, 13.3573, 13.5142, 14.1418,
15.0832, 11.1607, 11.1607, 11.1607, 11.1607, 11.3176, 11.3176,
11.3176, 11.9452, 11.7883, 11.7883, 11.7883, 11.9452, 12.4159,
12.5728, 13.3573, 13.3573, 13.3573, 13.0435, 12.8866, 12.8866,
12.8866, 12.8866, 12.8866, 12.8866, 12.8866, 12.8866, 12.8866,
12.8866, 12.8866, 12.8866, 12.8866, 12.8866, 12.8866, 12.8866,
11.0038, 11.0038, 11.0038, 11.0038, 11.0038, 11.0038, 11.0038,
11.0038, 10.8469, 10.8469, 10.8469, 10.8469, 10.8469, 10.8469,
10.8469, 10.8469, 9.121, 9.121, 9.121, 9.121, 9.121, 9.121, 9.121,
9.121, 5.0416, 5.0416, 5.0416, 5.0416, 5.0416, 5.0416, 5.0416,
5.0416, 5.0416, 5.0416, 3.3157, 3.3157, 3.3157, 3.3157, 3.1588,
3.1588, 3.1588, 3.1588, 1.5898, 1.7467, 1.7467, 1.7467, 1.7467,
1.7467, 0.8053, 0.8053, 0.8053, 0.8053, 0.8053, 0.8053, 0.8053,
0.8053, 0.8053, 0.8053, 2.845, 2.845, 2.845, 2.845, 2.845, 2.845,
2.845, 2.845, 2.845, 2.845, 2.845, 2.845, 2.845), Date.Time structure(c(2L,
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 7L, 7L, 7L, 7L, 7L, 7L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L), .Label = c("02/11/2010",
"04/05/2010", "07/06/2010", "08/12/2010",
"10/01/2011", "13/03/2011",
"15/02/2011", "15/07/2010", "19/08/2010",
"20/04/2011", "20/08/2010",
"23/10/2010", "28/09/2010", "30/09/2010"), class =
"factor"),
Month.Year = structure(c(5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("01,2011",
"02,2011", "03,2011", "04,2011",
"05,2010", "06,2010", "07,2010",
"08,2010", "09,2010", "10,2010",
"11,2010", "12,2010"), class "factor"),
Month = structure(c(9L, 9L, 9L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 4L, 4L, 4L, 4L,
4L, 4L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Apr",
"Aug", "Dec", "Feb", "Jan",
"Jul", "Jun", "Mar", "May",
"Nov",
"Oct", "Sep"), class = "factor"), Week =
c(19L, 19L, 19L,
24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L,
24L, 24L, 24L, 24L, 24L, 24L, 24L, 29L, 29L, 29L, 29L, 29L,
29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L,
29L, 29L, 29L, 29L, 29L, 29L, 34L, 34L, 34L, 34L, 34L, 34L,
34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L,
34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 40L,
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L,
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L,
40L, 40L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L,
43L, 43L, 43L, 43L, 43L, 43L, 45L, 45L, 45L, 45L, 45L, 45L,
45L, 45L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 8L, 8L, 8L, 8L, 8L, 8L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 17L, 17L, 17L,
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L), Shortdate structure(c(2L,
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 7L, 7L, 7L, 7L, 7L, 7L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L), .Label = c("02/11/2010",
"04/05/2010",
"07/06/2010", "08/12/2010", "10/01/2011",
"13/03/2011", "15/02/2011",
"15/07/2010", "19/08/2010", "20/04/2011",
"20/08/2010", "23/10/2010",
"28/09/2010", "30/09/2010"), class =
"factor")), .Names = c("ID",
"Temp", "Date.Time", "Month.Year",
"Month", "Week", "Shortdate"
), class = "data.frame", row.names = c(NA, -172L))
Then my ggplot is:
plot1<-ggplot(data=x,aes(Month.Year,Temp))+
+ geom_boxplot()
and my months are do not appear in chronological order in my plot
Cheers
Phil
--
View this message in context:
http://r.789695.n4.nabble.com/ggplot2-month-and-year-boxplot-x-axis-order-problem-tp3623029p3623342.html
Sent from the R help mailing list archive at Nabble.com.
philipharrison
2011-Jun-25 15:11 UTC
[R] ggplot2 month and year boxplot x axis order problem
Hi,
Thanks, although I dont think it was the Date.Time variable that was the
problem because my x variable was Month.Year.
I realise now that this month variable was a factor and I changed the order
of the factor using the following :
fish$MonthYr<-factor(fish$MOYR,levels=c("05,2010","06,2010","07,2010","08,2010","09,2010","10,2010","11,2010","12,2010","01,2011","02,2011","03,2011","04,2011"))
This seems to have ordered my x axis (factors)in the correct fashion.
Thanks again for your help
Phil
--
View this message in context:
http://r.789695.n4.nabble.com/ggplot2-month-and-year-boxplot-x-axis-order-problem-tp3623029p3624680.html
Sent from the R help mailing list archive at Nabble.com.