Dates as facets

1,381 views
Skip to first unread message

Sam Albers

unread,
Jun 5, 2012, 3:22:01 PM6/5/12
to ggplot2
Hello all,

I am wondering how folks deal with dates when they want to use them as a facetting variable. Is there a more elegant solution than simply ordering the date variable levels as a factor then plotting it like that? So for example:


## data loaded in with date in chronological order
df<-data.frame(x=runif(40,2,3), y=runif(40,2,3),
               date=rep(c("03-05-2011", "06-05-2011", "11-05-2011", "25-05-2011",
                          "04-10-2011","06-10-2011", "14-07-2011",  "28-09-2011"), each=5)
               )

## order is broken
ggplot(df, aes(y=y,x=x)) +
  geom_point() +
  facet_wrap(~date)

## convert to date class as normal
df$date.form <- as.Date(df$date, format="%d-%m-%Y")

## but ggplot2 doesn't like that as a facet
ggplot(df, aes(y=y,x=x)) +
  geom_point() +
  facet_wrap(~date.form)

This probably has come up before for other folks. Any suggestions?

Thanks in advance!

Sam

adam.l...@pnc.com

unread,
Jun 5, 2012, 3:59:44 PM6/5/12
to Sam Albers, ggplot2
Hi Sam,

I've run into the same issue. What I found is that if you leave the date formats intact, and simply use "as.factor" in the ggplot2 call, the intended order is preserved.

## convert to date class as normal
df$date.form <- as.Date(df$date, format="%d-%m-%Y")

## but ggplot2 doesn't like that as a facet
ggplot(df, aes(y=y,x=x)) +
  geom_point() +
  facet_wrap(~as.factor(date.form))

Adam Loveland



From: Sam Albers <tonights...@gmail.com>
To: ggplot2 <ggp...@googlegroups.com>
Date: 06/05/2012 03:22 PM
Subject: Dates as facets
Sent by: ggp...@googlegroups.com


--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example:
https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options:
http://groups.google.com/group/ggplot2


The contents of this email are the property of PNC. If it was not addressed to you, you have no legal right to read it. If you think you received it in error, please notify the sender. Do not forward or copy without permission of the sender. This message may contain an advertisement of a product or service and thus may constitute a commercial electronic mail message under US Law. The postal address for PNC is 249 Fifth Avenue, Pittsburgh, PA 15222. If you do not wish to receive any additional advertising or promotional messages from PNC at this e-mail address, click here to unsubscribe. https://pnc.p.delivery.net/m/u/pnc/uni/p.asp
By unsubscribing to this message, you will be unsubscribed from all advertising or promotional messages from PNC. Removing your e-mail address from this mailing list will not affect your subscription to alerts, e-newsletters or account servicing e-mails.


Sam Albers

unread,
Jun 5, 2012, 5:14:22 PM6/5/12
to adam.l...@pnc.com, ggplot2
Thanks for the response Adam. This doesn't appear to work as you
thought it might.

> ggplot(df, aes(y=y,x=x)) +
+ geom_point() +
+ facet_wrap(~as.factor(date.form))
Error in layout_base(data, vars, drop = drop) :
At least one layer must contain all variables used for facetting

What this what you meant?

adam.l...@pnc.com

unread,
Jun 5, 2012, 5:29:59 PM6/5/12
to Sam Albers, ggplot2
My apologies.

I recently confronted this issue, so I figured I'd respond. But in fact, this wasn't the solution (obviously). What I did, which worked for me, was similar to the following:

df$date.factor <-as.factor(df$date.form)

ggplot(df, aes(y=y,x=x)) +
   geom_point() +
   facet_wrap(~date.factor)

Point is, if your data arrives as factors, and you convert it to date form, converting it back will allow you to facet it and do so in the correct order.

Hope this helps. BTW, I searched the forum for a better answer than mine and didn't find one but would be interested to know if this issue is fixed somehow.

Adam Loveland


From: Sam Albers <tonights...@gmail.com>
To: adam.l...@pnc.com
Cc: ggplot2 <ggp...@googlegroups.com>
Date: 06/05/2012 05:14 PM
Subject: Re: Dates as facets
Sent by: ggp...@googlegroups.com

Sam Albers

unread,
Jun 5, 2012, 5:37:18 PM6/5/12
to adam.l...@pnc.com, ggplot2


On Tue, Jun 5, 2012 at 2:29 PM, <adam.l...@pnc.com> wrote:
My apologies.

None required. I should have pick up on this solution anyway. 

I recently confronted this issue, so I figured I'd respond. But in fact, this wasn't the solution (obviously). What I did, which worked for me, was similar to the following:

df$date.factor <-as.factor(df$date.form)

ggplot(df, aes(y=y,x=x)) +
   geom_point() +
   facet_wrap(~date.factor)

Point is, if your data arrives as factors, and you convert it to date form, converting it back will allow you to facet it and do so in the correct order.

Hope this helps. BTW, I searched the forum for a better answer than mine and didn't find one but would be interested to know if this issue is fixed somehow.

I also looked around but didn't see anything. Thanks so much!

Dennis Murphy

unread,
Jun 5, 2012, 6:09:21 PM6/5/12
to Sam Albers, adam.l...@pnc.com, ggplot2
Hi Sam:

Try this. Starting from your data frame df in the original post,

df$date.form <- as.Date(df$date, format="%d-%m-%Y")
df$date.fac <- as.factor(df$date.form)

> str(df)
'data.frame': 40 obs. of 5 variables:
$ x : num 2.53 2 2.86 2.33 2.33 ...
$ y : num 2.57 2.44 2.45 2.7 2.35 ...
$ date : Factor w/ 8 levels "03-05-2011","04-10-2011",..: 1 1 1 1
1 3 3 3 3 3 ...
$ date.form: Date, format: "2011-05-03" "2011-05-03" ...
$ date.fac : Factor w/ 8 levels "2011-05-03","2011-05-06",..: 1 1 1 1
1 2 2 2 2 2 ...

ggplot(df, aes(y = y, x = x)) +
geom_point() +
facet_wrap(~ date.fac)

See the attachment; is that what you were expecting?

R version 2.15.0 (2012-03-30)
Platform: x86_64-pc-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] plyr_1.7.1 scales_0.2.1 ggplot2_0.9.1


HTH,
Dennis
sam.png

Sam Albers

unread,
Jun 5, 2012, 6:32:25 PM6/5/12
to Dennis Murphy, adam.l...@pnc.com, ggplot2
Hi Dennis,

Yes this works well. I think I was hoping that there was some obscure
option in facet_wrap that went something like this

ggplot(df, aes(y = y, x = x)) +
geom_point() +
facet_wrap(~ date, date_scale="%b-%d")

So that is facet labels using the date class. But thanks for the
response. This will certainly suffice.

Cheers,

Sam

Dennis Murphy

unread,
Jun 5, 2012, 6:57:54 PM6/5/12
to Sam Albers, adam.l...@pnc.com, ggplot2
The scale_*() functions are meant for guides (positional axes or
legends). For strip labels, there are label_*() functions in the
scales package to accompany the labeller = argument in facet_grid(),
but there is no equivalent in facet_wrap() since it doesn't contain a
labeller = argument. If you think it's important enough, you could
raise it as a feature request for ggplot2:
https://github.com/hadley/ggplot2/issues

Dennis

Brian Diggs

unread,
Jun 7, 2012, 1:13:53 PM6/7/12
to Dennis Murphy, Sam Albers, ggplot2
On 6/5/2012 3:57 PM, Dennis Murphy wrote:
> The scale_*() functions are meant for guides (positional axes or
> legends). For strip labels, there are label_*() functions in the
> scales package to accompany the labeller = argument in facet_grid(),
> but there is no equivalent in facet_wrap() since it doesn't contain a
> labeller = argument. If you think it's important enough, you could
> raise it as a feature request for ggplot2:
> https://github.com/hadley/ggplot2/issues

Just as an FYI, there already is an issue for this:

https://github.com/hadley/ggplot2/issues/25

> Dennis
>
>
> On Tue, Jun 5, 2012 at 3:32 PM, Sam Albers<tonightsthenight-Re...@public.gmane.org> wrote:
>> Hi Dennis,
>>
>> Yes this works well. I think I was hoping that there was some obscure
>> option in facet_wrap that went something like this
>>
>> ggplot(df, aes(y = y, x = x)) +
>> geom_point() +
>> facet_wrap(~ date, date_scale="%b-%d")
>>
>> So that is facet labels using the date class. But thanks for the
>> response. This will certainly suffice.
>>
>> Cheers,
>>
>> Sam
>>
>>>>> To post: email ggplot2-/JYPxA39Uh5...@public.gmane.org
>>>>> To unsubscribe: email ggplot2+unsubscribe-/JYPxA39Uh5...@public.gmane.org
>>>>> More options: http://groups.google.com/group/ggplot2
>>>>>
>>>>>
>>>>> The contents of this email are the property of PNC. If it was not addressed to you, you have no legal right to read it. If you think you received it in error, please notify the sender. Do not forward or copy without permission of the sender. This message may contain an advertisement of a product or service and thus may constitute a commercial electronic mail message under US Law. The postal address for PNC is 249 Fifth Avenue, Pittsburgh, PA 15222. If you do not wish to receive any additional advertising or promotional messages from PNC at this e-mail address, click here to unsubscribe. https://pnc.p.delivery.net/m/u/pnc/uni/p.asp
>>>>> By unsubscribing to this message, you will be unsubscribed from all advertising or promotional messages from PNC. Removing your e-mail address from this mailing list will not affect your subscription to alerts, e-newsletters or account servicing e-mails.
>>>>>
>>>>>
>>>>
>>>> --
>>>> You received this message because you are subscribed to the ggplot2 mailing list.
>>>> Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
>>>>
>>>> To post: email ggplot2-/JYPxA39Uh5...@public.gmane.org
>>>> To unsubscribe: email ggplot2+unsubscribe-/JYPxA39Uh5...@public.gmane.org
>>>> More options: http://groups.google.com/group/ggplot2
>


--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University
Reply all
Reply to author
Forward
0 new messages