[R] Help me please: gplot, facets_wrap and ordering of x axis dates
John Kane
jrkrideau at inbox.com
Wed May 15 15:36:32 CEST 2013
My appologies, I was in a rush yesterday and a) completely misread the post and b) did not scroll down far enough. David W is correct that you need to do something with the data set. <.6 is not a number.
Also, as David says, it sounds like your coversion using as.Date did not work out. The difficulty from here is that if is graphs then it sounds like your actual data.frame is not exactly the same as what I get if I try to read in the data you included. hat is why supplying the data using something like dput() is important.
I don't see how got something to plot. However if I get rid of the < in the text file and read in the data , etc I get something that may look like what you want. See code below. I renamed the data.frame to dat1, just because it was handy and renamed the variable date to date1 since date is a reserved word in R . Type date() to see what I mean.
Again my applogies for completely misreading the problem
John Kane
Kingston ON Canada
#####################################################
library(lubridate)
library(ggplot2)
dat1 <- structure(list(id = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 4L,
4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L,
6L, 6L, 6L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 10L,
10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L,
12L, 12L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 17L, 17L, 18L, 18L, 18L,
18L, 18L), date1 = c("8/16/10", "10/25/10", "11/8/10", "11/22/10",
"12/6/10", "8/18/10", "10/25/10", "11/8/10", "11/22/10", "8/20/10",
"10/25/10", "11/8/10", "11/22/10", "12/6/10", "10/26/11", "6/4/09",
"6/18/10", "8/25/10", "9/15/10", "10/25/10", "11/8/10", "11/22/10",
"12/6/10", "11/4/11", "9/23/10", "10/25/10", "11/8/10", "11/22/10",
"12/6/10", "8/25/10", "10/25/10", "11/8/10", "12/6/10", "6/11/12",
"10/6/10", "11/22/10", "2/2/11", "2/16/11", "3/2/11", "3/16/11",
"9/14/10", "2/2/11", "2/16/11", "3/2/11", "3/16/11", "8/20/10",
"2/2/11", "2/16/11", "3/2/11", "3/16/11", "10/26/11", "12/14/10",
"2/2/11", "2/16/11", "3/2/11", "3/16/11", "11/13/09", "8/19/10",
"2/2/11", "2/6/13", "4/24/13", "8/18/10", "10/5/10", "10/27/10",
"2/2/11", "2/16/11", "3/2/11", "3/16/11", "6/29/11", "8/15/11",
"8/15/12", "10/31/12", "12/10/10", "2/2/11", "3/2/11", "3/16/11",
"12/17/10", "1/25/11", "2/2/11", "2/2/11", "2/16/11", "3/2/11",
"3/16/11", "3/20/12", "3/26/12", "3/30/12", "4/2/12", "4/23/12",
"11/17/11", "12/9/11", "2/25/13", "3/11/13", "3/25/13", "4/10/13",
"4/22/13"), value = c(0.16, 0.16, 0.42, 0.81, 0.16, 2.93, 2.4,
1.36, 1.22, 0.77, 0.85, 1.22, 0.21, 1.81, 0.54, 1.33, 1.32, 2.5,
1.3, 1.1, 0.66, 0.84, 7.42, 1.21, 0.97, 2.25, 0.51, 0.53, 0.41,
3.14, 3.58, 2.41, 2.08, 3.2, 0.24, 0.34, 0.58, 0.54, 0.25, 0.39,
0.28, 0.19, 0.42, 0.39, 0.26, 0.16, 0.16, 0.16, 0.16, 1.76, 0.16,
0.48, 1.2, 0.44, 0.32, 0.34, 0.73, 3.32, 13.7, 1.35, 0.85, 0.66,
0.68, 0.53, 0.54, 0.49, 0.31, 0.4, 0.53, 0.55, 0.94, 0.74, 0.4,
0.44, 0.38, 0.43, 0.18, 0.16, 0.53, 0.54, 0.46, 0.29, 0.2, 0.18,
0.23, 0.52, 0.33, 0.3, 3.35, 2.56, 18.1, 14.9, 11.1, 8.47, 15.9
)), .Names = c("id", "date1", "value"), class = "data.frame", row.names = c(NA,
-95L))
str(dat1)
dat1$date1 <- mdy(dat1$date1) # using lubridate
str(dat1) # POSIXct object not date object as in your original code
ggplot(data=dat1) + geom_line(aes(x=date1, y=value, group=id, colour=
factor(id))) + facet_wrap(~id, scales = "free")
#####################################################
> -----Original Message-----
> From: david_lyon3 at yahoo.com
> Sent: Wed, 15 May 2013 05:49:18 -0700 (PDT)
> To: jrkrideau at inbox.com, r-help at r-project.org
> Subject: Re: [R] Help me please: gplot, facets_wrap and ordering of x
> axis dates
>
> Thanks John for your reply.
> I did include the data if you scroll down to the end of my original
> email.
>
> Can someone help me on this?
>
> Thanks
>
>
>
>
> ----- Original Message -----
> From: John Kane <jrkrideau at inbox.com>
> To: David Lyon <david_lyon3 at yahoo.com>; r-help <r-help at r-project.org>
> Cc:
> Sent: Tuesday, May 14, 2013 4:11 PM
> Subject: RE: [R] Help me please: gplot, facets_wrap and ordering of x
> axis
> dates
>
> Thank you for supplying the code. It would be easier to help you if we
> also had some data to work with. ?dput
>
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> I think reorder() is likely to do the trick but I don't have enough time
> to mock up some data and check at the moment.
>
> Have a look at
> http://stackoverflow.com/questions/3744178/ggplot2-sorting-a-plot
>
> Good luck.
> John Kane
> Kingston ON Canada
>
>
>> -----Original Message-----
>> From: david_lyon3 at yahoo.com
>> Sent: Tue, 14 May 2013 12:59:07 -0700 (PDT)
>> To: r-help at r-project.org
>> Subject: [R] Help me please: gplot, facets_wrap and ordering of x axis
>> dates
>>
>> I have a text file of data as below and doing a ggplot line plot of all
>> the ids as separate mini line plots which works with the following code.
>>
>> Problem how do I order the dates for each id plot on the x axis so that
>> the dates are going from oldest to most recent????
>>
>>
>> Thanks in advance
>>
>> Dave
>>
>>
>>
>>
>>
>> CODE:
>>
>> a<-read.table("DATA",header=TRUE);
>> b<-a[order(as.Date(a$date, format="%m/%d/%Y")),]
>>
>> ggplot(data=b) + geom_line(aes(x=date, y=value, group=id, colour=
>> factor(id))) + facet_wrap(~id, scales = "free")
>>
>>
>>
>>
>>
>>
>> DATA:
>>
>>
>> id date value
>> 001 8/16/10 <0.16
>> 001 10/25/10 <0.16
>> 001 11/8/10 0.42
>> 001 11/22/10 0.81
>> 001 12/6/10 <0.16
>> 002 8/18/10 2.93
>> 002 10/25/10 2.4
>> 002 11/8/10 1.36
>> 002 11/22/10 1.22
>> 004 8/20/10 0.77
>> 004 10/25/10 0.85
>> 004 11/8/10 1.22
>> 004 11/22/10 0.21
>> 004 12/6/10 1.81
>> 004 10/26/11 0.54
>> 005 6/4/09 1.33
>> 005 6/18/10 1.32
>> 005 8/25/10 2.5
>> 005 9/15/10 1.3
>> 005 10/25/10 1.1
>> 005 11/8/10 0.66
>> 005 11/22/10 0.84
>> 005 12/6/10 7.42
>> 005 11/4/11 1.21
>> 006 9/23/10 0.97
>> 006 10/25/10 2.25
>> 006 11/8/10 0.51
>> 006 11/22/10 0.53
>> 006 12/6/10 0.41
>> 008 8/25/10 3.14
>> 008 10/25/10 3.58
>> 008 11/8/10 2.41
>> 008 12/6/10 2.08
>> 008 6/11/12 3.2
>> 009 10/6/10 0.24
>> 009 11/22/10 0.34
>> 009 2/2/11 0.58
>> 009 2/16/11 0.54
>> 009 3/2/11 0.25
>> 009 3/16/11 0.39
>> 010 9/14/10 0.28
>> 010 2/2/11 0.19
>> 010 2/16/11 0.42
>> 010 3/2/11 0.39
>> 010 3/16/11 0.26
>> 011 8/20/10 <0.16
>> 011 2/2/11 <0.16
>> 011 2/16/11 <0.16
>> 011 3/2/11 <0.16
>> 011 3/16/11 1.76
>> 011 10/26/11 <0.16
>> 012 12/14/10 0.48
>> 012 2/2/11 1.2
>> 012 2/16/11 0.44
>> 012 3/2/11 0.32
>> 012 3/16/11 0.34
>> 013 11/13/09 0.73
>> 013 8/19/10 3.32
>> 013 2/2/11 13.7
>> 014 2/6/13 1.35
>> 014 4/24/13 0.85
>> 014 8/18/10 0.66
>> 014 10/5/10 0.68
>> 014 10/27/10 0.53
>> 014 2/2/11 0.54
>> 014 2/16/11 0.49
>> 014 3/2/11 0.31
>> 014 3/16/11 0.4
>> 014 6/29/11 0.53
>> 014 8/15/11 0.55
>> 014 8/15/12 0.94
>> 014 10/31/12 0.74
>> 015 12/10/10 0.4
>> 015 2/2/11 0.44
>> 015 3/2/11 0.38
>> 015 3/16/11 0.43
>> 016 12/17/10 0.18
>> 016 1/25/11 <0.16
>> 016 2/2/11 0.53
>> 016 2/2/11 0.54
>> 016 2/16/11 0.46
>> 016 3/2/11 0.29
>> 016 3/16/11 0.2
>> 016 3/20/12 0.18
>> 016 3/26/12 0.23
>> 016 3/30/12 0.52
>> 016 4/2/12 0.33
>> 016 4/23/12 0.3
>> 017 11/17/11 3.35
>> 017 12/9/11 2.56
>> 018 2/25/13 18.1
>> 018 3/11/13 14.9
>> 018 3/25/13 11.1
>> 018 4/10/13 8.47
>> 018 4/22/13 15.9
>>
>>
>> ______________________________________________
>> 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.
>
> ____________________________________________________________
> FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!
> Check it out at http://www.inbox.com/earth
____________________________________________________________
GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at http://www.inbox.com/smileys
Works with AIM®, MSN® Messenger, Yahoo!® Messenger, ICQ®, Google Talk™ and most webmails
More information about the R-help
mailing list