How to get discrete dates in x scale?

1,956 views
Skip to first unread message

waschbaer

unread,
Feb 9, 2010, 11:49:50 PM2/9/10
to ggplot2
Hi @all

I am trying to set up a graph showing some time-series data for
nutrient concentrations over a specific time. It works all right for
me up to the stage where I want to have my six sampling dates as
discrete x axis labels on my x axis. I had a look on the internet for
a couple of hours now but I couldn't find anything on how to actually
get this done. I tried scale_x_date(breaks = as.Date ("2008-10-20",
"2009-01-27", plus remaining dates) but it didn't change the scale
from the automated format to the discrete format I would like to have.
I also tried to set limits and I also tried to use major = as.Date and
then my specific date but nothing worked. Any ideas? Thanks in
advance!

Here is the code:

read.csv("http://dl.dropbox.com/u/4236038/seasonal_var_sixmile.csv") -
> seasonal
melt_seasonal <- melt(seasonal, id=c("ID", "date"), na.rm=TRUE)
melt_seasonal$date<- as.Date(melt_seasonal$date)
melt_seasonal$ID <- as.factor(melt_seasonal$ID)
p <- ggplot(melt_seasonal, aes(x = date, y = value))
p +geom_line(aes(color=factor(ID))) + geom_point (aes(shape =
factor(ID))) +scale_shape(solid=FALSE) + facet_grid(variable~., scales
= "free")

James Howison

unread,
Feb 10, 2010, 12:48:13 AM2/10/10
to ggplot2
I would have expected + scale_x_datetime(format="%Y-%m-%d") to work, but that doesn't work for me, I get dates in 1969 doing that. the date column does seem to be in Date format ...

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> To post to this group, send email to ggp...@googlegroups.com
> To unsubscribe from this group, send email to
> ggplot2+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/ggplot2

David Kahle

unread,
Feb 10, 2010, 1:21:07 AM2/10/10
to James Howison, ggplot2
I would have thought the scale_date would have worked with the breaks, but it didn't work for me either.  One way to get around it is to just toss the higher class of dates and manually work with numerics and factors, and use the scales accordingly.  Below is a sketch of how it can be done.  It's not pretty, but you can massage it into whatever you had wanted before.

df  <- read.csv('seasonal_var_sixmile.csv')
mdf <- melt(df, id = c('ID', 'date'), na.rm = TRUE)

mdf$date <- as.Date(mdf$date)
mdf$ID   <- factor(mdf$ID)
dates    <- unique(mdf$date)
mdf$date <- as.numeric(mdf$date)
dates_num <- unique(mdf$date)

ggplot(aes(x = date, y = value), data = mdf) + 
  geom_line(aes(colour = ID)) +
  geom_point(aes(shape = ID)) + 
  facet_grid(facets = variable ~ ., scales = 'free') +
  scale_x_continuous(labels = dates, breaks = dates_num) +
  opts(axis.text.x
    theme_text(angle = -45, lineheight = 3))

Hope it helps
david.

waschbaer

unread,
Feb 10, 2010, 1:31:13 AM2/10/10
to ggplot2
Hi David and James

Thanks a lot for your quick replies. I didn't know that my question is
such an unusual task for ggplot. Isn't it something normal what I
would ggplot like to do? In excel you can do that in a second but with
ggplot I was looking on the web for ages and couldn't find a proper
solution.

I will give it a go and see what R does.

Cheers
Martin

hadley wickham

unread,
Feb 10, 2010, 3:42:43 PM2/10/10
to waschbaer, ggplot2
Hi Martin,

Just convert your date to a factor:

ggplot(melt_seasonal, aes(x = factor(date), y = value, group = ID)) +


geom_line(aes(color=factor(ID))) +
geom_point(aes(shape = factor(ID))) +
scale_shape(solid=FALSE) +
facet_grid(variable ~ . , scales = "free")

Strangely enough replicating excel is not a major motivation for ggplot2 ;)

Hadley

--
http://had.co.nz/

James Howison

unread,
Feb 10, 2010, 4:24:37 PM2/10/10
to ggplot2
That's a little non-intuitive (to me at least). A datetime isn't a factor, it's a datetime, right? Is this expected behavior or a work-around?

hadley wickham

unread,
Feb 10, 2010, 4:37:54 PM2/10/10
to James Howison, ggplot2
> That's a little non-intuitive (to me at least).  A datetime isn't a factor, it's a datetime, right?  Is this expected behavior or a work-around?

But you don't want a date to act like a date - because you want the
same amount of space between each date, not an amount of space
proportional to the time between those dates. You want a date to
behave like a factor, so you convert it to one.

Hadley

James Howison

unread,
Feb 10, 2010, 4:45:11 PM2/10/10
to ggplot2

On Feb 10, 2010, at 13:37, hadley wickham wrote:

>> That's a little non-intuitive (to me at least). A datetime isn't a factor, it's a datetime, right? Is this expected behavior or a work-around?
>
> But you don't want a date to act like a date - because you want the
> same amount of space between each date, not an amount of space
> proportional to the time between those dates. You want a date to
> behave like a factor, so you convert it to one.

Ah, I can see that. But do you know why adding + scale_x_datetime(format="%Y-%m-%d") to the original commands seemed to produce dates in 1969?

Thx,
James

hadley wickham

unread,
Feb 10, 2010, 4:56:37 PM2/10/10
to James Howison, ggplot2
>> But you don't want a date to act like a date - because you want the
>> same amount of space between each date, not an amount of space
>> proportional to the time between those dates.  You want a date to
>> behave like a factor, so you convert it to one.
>
> Ah, I can see that.  But do you know why adding + scale_x_datetime(format="%Y-%m-%d") to the original commands seemed to produce dates in 1969?

I think you probably wanted scale_x_date because the objects were
dates, not POSIXct date times.

Hadley

--
http://had.co.nz/

James Howison

unread,
Feb 10, 2010, 4:59:45 PM2/10/10
to ggplot2

That'd be it :)

--J

waschbaer

unread,
Feb 10, 2010, 5:01:11 PM2/10/10
to ggplot2
Sorry for referring to excel but I think that was just a frustrating
reaction of a newbie ;)

waschbaer

unread,
Feb 10, 2010, 6:57:18 PM2/10/10
to ggplot2
Hi Hadley

That is exactly the scale that I wanted. Strangely enough I think I
tried date as a factor but I might have done it the wrong way.

Thanks a lot for helping me.

Reply all
Reply to author
Forward
0 new messages