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")
> --
> 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
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
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
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
>> 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
I think you probably wanted scale_x_date because the objects were
dates, not POSIXct date times.
Hadley
That'd be it :)
--J
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.