How to add geom_line with dataset of different length ?

2,814 views
Skip to first unread message

mateus...@gmail.com

unread,
Jul 15, 2011, 11:12:30 AM7/15/11
to ggplot2
Hi,

I have one dataset with factors Subject, Time which has length Subjects*Time
and then I have another dataset Average with columns Time, Value
I do qplot(Time,value, color=Subject, data=A) to plot first dataset, and I would like to add line geom_line(aes(x=Time,y=Value),data=Average)
how I get error
 arguments imply differing number of rows: 64, 717

Any ideas how to solve this ? I just want to add new data from dataset of different size, however levels of factor Time are the same and it should not matter.

Thanks for help


Hadley Wickham

unread,
Jul 15, 2011, 1:10:55 PM7/15/11
to mateus...@gmail.com, ggplot2
Please provide a reproducible example:
https://github.com/hadley/devtools/wiki/Reproducibility
Hadley

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

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

Dennis Murphy

unread,
Jul 15, 2011, 1:18:52 PM7/15/11
to mateus...@gmail.com, ggplot2
In the absence of a reproducible example, the following is untested,
so caveat emptor. This looks like a spaghetti plot with superimposed
average line, similar to what is found in section 4.9.3 of Hadley's
book.

For your particular case, I'd suggest plotting the two data frames in
different geom_line() layers, something like

ggplot() + geom_line(data = ds1, aes(x = Time, y = Value, colour = Subject)) +
geom_line(data = Average, aes(x = Time, y = Value), size = 1)

# default black color, change it to whatever you wish *outside* the
aes() arguments.

This approach allows you to combine information from different data
sets (with possibly different variable names) in a single plot.
Fortunately, the x and y variable names are the same (with related
information) in the two data frames, so there shouldn't be much
problem in coordinating the two.

However, there is a sneakier way to do this with only the first data
set (using the group = 1 trick to obtain the average line),
illustrated in section 4.9.3 of Hadley's book. Try the last set of
code chunks from the scripts in Chapter 4 of the book,
http://had.co.nz/ggplot2/book/layers.r

Start from here (about 10cm from the bottom):
require(nlme, quiet = TRUE, warn.conflicts = FALSE)

Copy and paste the code from that point on into your R session, one
plot at a time, and see what comes out. It's an instructive chunk of
code.

HTH,
Dennis

Reply all
Reply to author
Forward
0 new messages