Broken axis possible?

1,020 views
Skip to first unread message

Bogaso

unread,
Jan 12, 2010, 6:48:59 AM1/12/10
to ggplot2
Hi all,

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,

hadley wickham

unread,
Jan 12, 2010, 8:52:24 AM1/12/10
to Bogaso, ggplot2
> 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?

What is a discontinuous time series plot?

Hadley


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

baptiste auguie

unread,
Jan 12, 2010, 9:03:48 AM1/12/10
to hadley wickham, ggplot2
> What is a discontinuous time series plot?

Never watched Futurama?

http://en.wikipedia.org/wiki/Time_Keeps_On_Slippin'

;)

David Kahle

unread,
Jan 12, 2010, 9:16:01 AM1/12/10
to hadley wickham, Bogaso, ggplot2
I don't really understand your example, but if all you want is breaks in the line, just set those observations to NA rather than removing them, and then the geom will do the rest.  So, something like

df <- data.frame(x = 1:10, y = c(1:3, NA, NA, 6:10))
qplot(x, y, data = df, geom = 'line')

as opposed to

df <- data.frame(x = c(1:3, 6:10), y = c(1:3, 6:10))
qplot(x, y, data = df, geom = 'line')

Is that what you want?

david. 

--
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

Bogaso

unread,
Jan 12, 2010, 9:47:48 AM1/12/10
to ggplot2
sorry for that complecated example. Please take this simple one :

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 -

ONKELINX, Thierry

unread,
Jan 12, 2010, 9:56:08 AM1/12/10
to Bogaso, ggplot2
Instead of removing the data from the axis I would rather use NA values for the missing months and add points to the line.

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.

hadley wickham

unread,
Jan 12, 2010, 10:08:44 AM1/12/10
to Bogaso, ggplot2
On Tue, Jan 12, 2010 at 8:47 AM, Bogaso <bogaso.c...@gmail.com> wrote:
> sorry for that complecated example. Please take this simple one :
>
> 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)))

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

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

Bogaso

unread,
Jan 12, 2010, 12:17:35 PM1/12/10
to ggplot2
Thanks Thierry and Hadley for your replies. NA could be one approach
however I actually want to **hide** some sensitive dates from direct
displaying because of some internal issues.

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:

hadley wickham

unread,
Jan 12, 2010, 12:31:48 PM1/12/10
to Bogaso, ggplot2
> 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?

Oops, that should be:

ggplot(df, aes(factor(date), value, group = 1)) + geom_line()

Hadley

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

Bogaso

unread,
Jan 12, 2010, 2:48:23 PM1/12/10
to ggplot2
Thanks Hadley for this reply. This is what I wanted, however it brings
some other problems as well like :

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,

hadley wickham

unread,
Jan 20, 2010, 10:49:26 AM1/20/10
to Bogaso, ggplot2
> 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()

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

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

Reply all
Reply to author
Forward
0 new messages