Disconnecting Nonconsecutive Points Using Line Graph

2,351 views
Skip to first unread message

Alexa

unread,
Sep 27, 2010, 10:13:44 AM9/27/10
to ggplot2
Hey all,

I'm trying to make a line graph where the x-axis is the date. For some
groups of points I have consecutive days with data which I would like
to be connected by a line. I also have data points that have several
days between the observations which I would like to show up as points
that aren't connected by a line. I'm not sure how to do this.

Right now my code looks like this:


DF <- read.table("LightCurve.csv", header=TRUE, sep=",")
error <-read.table("LCerror.csv", header = TRUE, sep=",")

#Remove NA's from data

new.data <- melt(DF, id.var= "X", na.rm = TRUE)

ggplot(new.data, aes(x = X, y= -value, linetype = variable)) +
geom_line()


Thanks!
Alexa

Ista Zahn

unread,
Sep 27, 2010, 10:22:35 AM9/27/10
to Alexa, ggplot2
Hi Alexa,
It would help if you could provide a reproducible example, either by
constructing an example dataset that illustrates the issue, or by
providing (possibly a subset of) your data.

Best,
Ista

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org

Alexa

unread,
Sep 27, 2010, 10:48:06 AM9/27/10
to ggplot2
Hi Ista,

So say this is my data set:

Day Count
1 5
2 6
3 8
7 10
8 11
9 12
10 13
12 14
13 15

I want days 1,2,3 connected but not 3 and 7. This is because they are
non consecutive, I don't know what the data for days 4,5, and 6 are
and I don't want the graph to try to guess. But then 7-10 would be
connected, have a gap indicating that there is no data for day 11, and
then start the line again at 12.

My real data set is:

X variable value
1 5154 MMO.645. 11.88570
2 5156 MMO.645. 11.17071
3 5206 MMO.645. 10.91310
4 5211 MMO.645. 10.78260
5 5213 MMO.645. 11.13900
6 5220 MMO.645. 11.88436
7 5228 MMO.645. 12.08520
8 5229 MMO.645. 11.63700
9 5232 MMO.645. 11.07582
10 5246 MMO.645. 11.75875
11 5247 MMO.645. 12.40000
12 5248 MMO.645. 12.43467
13 5266 MMO.645. 12.68591
14 5272 MMO.645. 12.94844
15 5273 MMO.645. 13.02550
16 5274 MMO.645. 12.96700
17 5280 MMO.645. 11.16925
18 5288 MMO.645. 12.06700
19 5289 MMO.645. 12.25700
20 5298 MMO.645. 12.97700
21 5299 MMO.645. 12.85467
22 5300 MMO.645. 11.91671
23 5301 MMO.645. 11.48125
24 5302 MMO.645. 11.41100
25 5307 MMO.645. 12.19900
26 5310 MMO.645. 12.00725
27 5197 AKO.645. 12.31400
28 5198 AKO.645. 12.32550
29 5201 AKO.645. 11.39167
30 5202 AKO.645. 11.34000
31 5206 AKO.645. 10.88800
32 5208 AKO.645. 10.88133
33 5240 AKO.645. 10.78600
34 5241 AKO.645. 10.94200
35 5242 AKO.645. 11.32450
36 5243 AKO.645. 11.86000
37 5245 AKO.645. 11.62500
38 5246 AKO.645. 11.76800
39 5247 AKO.645. 12.45100
40 5259 AKO.645. 11.51100
41 5260 AKO.645. 11.76300
42 5261 AKO.645. 11.71300
43 5267 AKO.645. 12.66100
44 5268 AKO.645. 12.87600
45 5269 AKO.645. 13.05050
46 5271 AKO.645. 13.10500
47 5272 AKO.645. 13.15550
48 5273 AKO.645. 13.17900
49 5274 AKO.645. 13.06250
50 5276 AKO.645. 12.76900
51 5277 AKO.645. 12.14900
52 5280 AKO.645. 11.16650
53 5284 AKO.645. 11.98500
54 5285 AKO.645. 11.63250
55 5290 AKO.645. 12.12600
56 5291 AKO.645. 12.30350
57 5293 AKO.645. 13.43700
58 5294 AKO.645. 13.47200
59 5295 AKO.645. 13.31700
60 5297 AKO.645. 13.29150
61 5298 AKO.645. 13.09350
62 5300 AKO.645. 11.94000
63 5311 AKO.645. 12.07000
64 5312 AKO.645. 11.96250

ONKELINX, Thierry

unread,
Sep 27, 2010, 11:10:05 AM9/27/10
to Alexa, ggplot2
Dear Alexa,

You need to add a grouping variable to your dataset. Then add group = your_grouping_variable to the aesthetics.

library(ggplot2)
dataset <- data.frame(Day = 1:30, Measure = rnorm(30))
dataset$Connected <- 0
dataset$Connected[9:13] <- 1
dataset$Connected[14:30] <- 2
ggplot(dataset, aes(x = Day, y = Measure, group = Connected)) + geom_line()

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 Alexa
> Verzonden: maandag 27 september 2010 16:48
> Aan: ggplot2
> Onderwerp: Re: Disconnecting Nonconsecutive Points Using Line Graph

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,
Sep 27, 2010, 12:09:03 PM9/27/10
to Alexa, ggplot2
> I want days 1,2,3 connected but not 3 and 7. This is because they are
> non consecutive, I don't know what the data for days 4,5, and 6 are
> and I don't want the graph to try to guess. But then 7-10 would be
> connected, have a gap indicating that there is no data for day 11, and
> then start the line again at 12.

The only way I know how to do this is to add in the implicit missings:

library(ggplot2)
library(plyr)

df <- data.frame(day = sample(30, 25), measure = rnorm(25))
all_days <- data.frame(day = seq(min(df$day), max(df$day)))
complete_df <- join(all_days, df)

qplot(day, measure, data = complete_df, geom = "line")

I don't see any way for ggplot2 to work this out automatically - how
could it know that you care about consecutive days instead of hours or
minutes or something else?

Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Brandon Hurr

unread,
Sep 27, 2010, 12:13:16 PM9/27/10
to Hadley Wickham, Alexa, ggplot2
How about if your x-axis was a discrete axis instead of a continuous one? Could breaks be implemented more easily there? 

Brandon


--

Alexa Villaume

unread,
Sep 30, 2010, 9:16:15 PM9/30/10
to Brandon Hurr, Hadley Wickham, ggplot2
Hey all,

Thanks for the help. Turns out the solution is to set the NAs to false when I melt the data. The line of code is - 

new.data<-melt(DF,id.var="X",na.rm=FALSE)

Alexa
Reply all
Reply to author
Forward
0 new messages