Is it possible to plot a time series with discontinuous data? For
example please consider following data :
library(zoo); library(ggplot2)
temp1 <- seq(as.Date("2008-01-01"), by="1 month", length.out = 14)[-c
(3,4)]
dat <- matrix(rnorm(12*10), nrow=12); dat[c(1,4,6), c(2,6,4)] = NA;
dat1 <- zoo(dat, as.yearmon(temp1))
colnames(dat1) <- paste("point", 1:10, sep="")
dat11 <- melt(coredata(dat1))
ggplot(dat11) + geom_point(aes(y=value, x=as.numeric(index(dat1))),
colour="black")
Here in the index of dat1, I have intentionally omitted 2 months,
namely March & april for 2008. Therefore my data is discontinuous.
However ggplot taking all months and displaying a continuous plot. How
to pass this information to ggplot to have a discontinuous time series
plot?
Thanks,
What is a discontinuous time series plot?
Hadley
Never watched Futurama?
http://en.wikipedia.org/wiki/Time_Keeps_On_Slippin'
;)
--
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
temp1 <- seq(as.Date("2008-01-01"), by="1 month", length.out = 14)[-c
(3,4)]
dat1 <- zoo(rnorm(length(temp1)), temp1); dat1
ggplot() + geom_line(aes(y=coredata(dat1), x=index(dat1)))
I do not want to show March-2008 and April-2008 in x-axis, because in
"dat1" those dates are not present. Any idea?
Thanks,
> >http://groups.google.com/group/ggplot2- Hide quoted text -
>
> - Show quoted text -
temp1 <- seq(as.Date("2008-01-01"), by="1 month", length.out = 14)
dat1 <- zoo(rnorm(length(temp1)), temp1);
dat1
dataset <- data.frame(index = index(dat1), core = coredata(dat1))
dataset$core[3:4] <- NA
ggplot(dataset, aes(x = index, y = core)) + geom_line() + geom_point()
HTH,
Thierry
----------------------------------------------------------------------------
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium
Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium
tel. + 32 54/436 185
Thierry....@inbo.be
www.inbo.be
To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data.
~ Roger Brinner
The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey
-----Oorspronkelijk bericht-----
Van: ggp...@googlegroups.com [mailto:ggp...@googlegroups.com] Namens Bogaso
Verzonden: dinsdag 12 januari 2010 15:48
Aan: ggplot2
Onderwerp: Re: Broken axis possible?
Thanks,
Druk dit bericht a.u.b. niet onnodig af.
Please do not print this message unnecessarily.
Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in this message
and any annex are purely those of the writer and may not be regarded as stating
an official position of INBO, as long as the message is not confirmed by a duly
signed document.
This is poor plot construction, which is likely to lead to problems in
the future. You should ALWAYS supply a data frame (and in this
example using zoo just complicates things):
df <- data.frame(
date = seq(as.Date("2008-01-01"), by="1 month", length.out = 14)[-c(3,4)],
value = rnorm(12)
)
ggplot(df, aes(date, value)) + geom_line()
And to fix your problem, you just want:
ggplot(df, aes(date, factor(value))) + geom_line()
Hadley
When I run Hadley's code I got a funny plot, wherein x-axis is "date"
and y-axis is "factor(value)" and there are no line, point nothing for
display. Is there any typo in that code? How can I correct that?
Thanks,
On Jan 12, 8:08 pm, hadley wickham <h.wick...@gmail.com> wrote:
Oops, that should be:
ggplot(df, aes(factor(date), value, group = 1)) + geom_line()
Hadley
If I change the format of Date then the sequence of date is getting
altered. For example :
df1 <- data.frame(
date1 = format(as.yearmon(seq(as.Date("2008-01-01"), by="1 month",
length.out = 26)[-c(3,4)]), "%b %y"),
value = rnorm(24)
); df1
ggplot(df1, aes(factor(date1), value, group = 1)) + geom_line()
Sequence of date is getting altered once I changed the format of Date.
Other problems are :
1. How to control the vertical gridlines? I have tried with
"scale_x_continuous(breaks = as.numeric(df1$date1[c(6,12,18)]))" but
getting error on that.
2. I cant get access of x-coordinates. For example if I want to draw a
rectangle using "annotate" then how should I specify the x-
coordinates?
I would be really grateful if you please help me out.
PS: Can you please explain this portion of above code "aes(factor
(date1), value, group = 1)" i.e. why you bring "group=1" here? I am
really stumbled because I dont see any of the help-pages explain that
thing.
Thanks,
First, keep your date as a date:
df1 <- data.frame(
date1 = seq(as.Date("2008-01-01"), by="1 month", length.out = 26)[-c(3,4)],
value = rnorm(24)
)
Then when you convert it to a factor, make sure to specify the desired ordering:
formatted_date <- format(df1$date, "%b %y")
df1$date_factor <- factor(formatted_date, levels = unique(formatted_date))
> Other problems are :
> 1. How to control the vertical gridlines? I have tried with
> "scale_x_continuous(breaks = as.numeric(df1$date1[c(6,12,18)]))" but
> getting error on that.
Because it's not a continuous variable - it's a discrete variable.
Use scale_x_discrete.
> 2. I cant get access of x-coordinates. For example if I want to draw a
> rectangle using "annotate" then how should I specify the x-
> coordinates?
They are integers starting at one from the left. You are basically
creating your own coordinate system, so it's up to you to manage
position.
> PS: Can you please explain this portion of above code "aes(factor
> (date1), value, group = 1)" i.e. why you bring "group=1" here? I am
> really stumbled because I dont see any of the help-pages explain that
> thing.
The group aesthetic tells ggplot how to divide up each geom (i.e. how
many different lines to draw). By default the group is the
combination of all discrete variables on the plot, but here you only
want one line, so you set group to a constant.
Hadley