Discrepant result between point and line graph with same data

9 views
Skip to first unread message

Alexa

unread,
Sep 1, 2010, 8:51:36 PM9/1/10
to ggplot2
Hey everyone,

I'm trying to make a line graph that plots two different sets of data.
The problem I'm having is that much of the data isn't being included
in the graph and what does show up is a mix between the two data sets.
When I do the same thing, only changing geom_line to geom_point, all
the data is graphed. I need it to be a line graph where the two lines
are represented differently, which I haven't included yet. My code is:


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

MMO <- DF$MMO.645
AKO <- DF$AKO.645
JD <- DF$X

Mag <- c(MMO,AKO)

G <- ggplot(data = DF, aes(x = JD, y = -Mag)) +
geom_line() +
scale_y_continuous("Magnitude Difference") +
scale_x_continuous("JD - 2450000") +
coord_cartesian()

My data frame is:

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

Alexa

unread,
Sep 1, 2010, 10:02:11 PM9/1/10
to ggplot2
Okay. so I just realized only consecutive dates are being connected.
Anyway to fix this?

Jonathan Christensen

unread,
Sep 1, 2010, 10:08:46 PM9/1/10
to Alexa, ggplot2
Alexa,

This problem is being caused by the presence of NAs in your dataset. To make the data easier to work with, I melted it (see the reshape package) and used the na.rm argument to remove the NAs (see ?melt):

data <- read.table("C:\\Users\\Jonathan\\Desktop\\temp.txt")

library(reshape)
library(ggplot2)

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

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

Jonathan


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

Reply all
Reply to author
Forward
0 new messages