Ploting geom_line with missing values

2,300 views
Skip to first unread message

mateus...@gmail.com

unread,
Apr 28, 2011, 8:47:43 AM4/28/11
to ggp...@googlegroups.com
Hi,

I have a simple function

qplot(factor(RTime),mean,data=ds,color=Treatment,na.rm=T)

which plots RTime vs mean for each Treatment.

It works great with geom='point', but now I would like to connect all points that belong to the same Treatment even though that there are missing values marked as NAs.
For example if there is point A B C NA F, I would like to connect C and F with stright line.

If I try to use geom='line' with this function I get empty plot. Can someone help me with that ? This is very old issue that I keep encountering over and over.

Thanks in advance,
Mateusz

Charlotte Wickham

unread,
Apr 28, 2011, 11:44:52 AM4/28/11
to mateus...@gmail.com, ggp...@googlegroups.com
Remember: Please provide a reproducible example: http://gist.github.com/270442

# Some data that may look like yours:
ds <- data.frame(RTime = rep(letters[1:5], each = 5),
Treatment = rep(paste("t", 1:5, sep = ""), 5), mean = rnorm(25))

# put in some missing means
ds[c(15, 10, 8, 1, 7, 11, 4, 5, 6, 13), "mean"] <- NA

# current plot
qplot(factor(RTime),mean,data=ds,color=Treatment)

# illustrates the problem?
qplot(factor(RTime),mean,data=ds,color=Treatment, geom = "line")

# you need to specify group since your x values are factors
qplot(factor(RTime),mean,data=ds,color=Treatment, geom = "line", group
= Treatment)
# but this doesn't solve the problem

# a missing point, NA, will break a line so,
# I think the easiest way is to get rid of the missing rows then plot:
ds.no.missing <- subset(ds, !is.na(mean))
qplot(factor(RTime),mean,data=ds.no.missing,color=Treatment,na.rm=T,
geom = "line", group = Treatment) +
geom_point()

Hope that helps,
Charlotte

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

mateus...@gmail.com

unread,
Apr 28, 2011, 12:09:25 PM4/28/11
to Charlotte Wickham, ggp...@googlegroups.com
Dear Charlotte,

First thanks for Your time and suggestions.
Yes this indeed illustrates my problem, however skipping NAs is very wrong, because then You're not anymore comparing correct values for given x.  This plot will be misleading in any case of ordered measurement for which You have index on x axis.
For example You could draw a very wrong conclusion about significant difference at index-es which are really different, while thinking that they are the same.

What I would like to achieve is to connect two available points with a straight line (to observe pattern), with correct fixed x indexes on x-axis. This is re-occurring problem for me, which I haven't solved yet with ggplot2.

I would be very grateful if someone would knew how to make exactly such a plot.

Thanks
Mateusz

Charlotte Wickham

unread,
Apr 28, 2011, 12:47:57 PM4/28/11
to mateus...@gmail.com, ggp...@googlegroups.com
I'm afraid I don't understand what you are trying to do. What do you
mean by "correct fixed x indexes"? Can you sketch the plot you are
trying to make?

Maybe, I misinterpreted "A B C NA F" as times, but really they are
treatments? Does my example data correctly represent yours?

Charlotte

The-plotter

unread,
Apr 13, 2012, 2:25:29 PM4/13/12
to ggp...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages