Problem with plotting two matrices and using a loop

26 views
Skip to first unread message

silke

unread,
Jan 21, 2009, 1:03:38 PM1/21/09
to ggplot2
Hi everyone,
I am trying to "translate" a plot into a ggplot to keep my graphics
consistent, but I am facing some problems and hope somebody can help.
I would like to plot two matrices against each other, like in a
scatterplot. This works easily with plot, for example:
x = matrix(rnorm(100),nrow=10)
y = matrix(rnorm(100),nrow=10)
plot(x,y)
or as well in a loop:
plot(x[1,], y[1,],type="n") # maybe with xlim,ylim argument
for (i in 1:10) {
points(x[i,], y[i,]) }

However, I don't get this to work with ggplot.
Obviously, I cannot just plot the matrices as above like "qplot(x,y)"
as I get an error message, I do not really understand why.
I have tried
xy = data.frame(x,y)
m = ggplot(xy)
m + for (i in 1:10) {
geom_point(data=dafr,aes(x=x[,i],y=y[,i]))
}
But then only the last layer (i=10) of the loop is plotted. If I would
explicitly write all ten loops it works as I want, but my matrices are
actually 160x160, so I would really like to find a better solution.
I have tried something with print() because I've read that it is
needed within loops, but it did not work either.
Any hint would be very much appreciated!

Thanks, Silke.

silke

unread,
Jan 22, 2009, 5:13:04 AM1/22/09
to ggplot2
Hallo again,
I just saw a little mistake in my yesterday's example of what I tried
that didn't work, i meant:
xy = data.frame(x,y)
m = ggplot(xy)
m + for (i in 1:10) {
geom_point(data=xy,aes(x=x[,i],y=y[,i]))
}
But still it does not work and only the last layer is printed -
doesn't anybody have a solution for me in this?
Thank you very much in advance,
Silke Schmidt.

hadley wickham

unread,
Jan 22, 2009, 8:13:19 AM1/22/09
to silke, ggplot2
On Thu, Jan 22, 2009 at 4:13 AM, silke <sil...@gmail.com> wrote:
>
> Hallo again,
> I just saw a little mistake in my yesterday's example of what I tried
> that didn't work, i meant:
> xy = data.frame(x,y)
> m = ggplot(xy)
> m + for (i in 1:10) {
> geom_point(data=xy,aes(x=x[,i],y=y[,i]))
> }
> But still it does not work and only the last layer is printed -
> doesn't anybody have a solution for me in this?
> Thank you very much in advance,

Have a look at what this code does:

a <- 1
a + for(i in 1:10) {a}

A for loop returns the last value. You want something like:

for (i in 1:10) {
print(m + geom_point(data=xy,aes(x=x[,i],y=y[,i])))
}

Regards,

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

silke

unread,
Jan 22, 2009, 11:04:51 AM1/22/09
to ggplot2
Thank you very much for your response, although the loop did not
really work for me as it returns 10 different plots, one of each
layer, and what I wanted is to have all layers in the same plot.
However I found another solution with the help of a friend:

x = as.numeric(matrix(rnorm(100),nrow=10))
y = as.numeric(matrix(rnorm(100),nrow=10) )
xy = data.frame(x,y)
m = ggplot(xy)
m + geom_point(aes(x=x,y=y))

Seems to be a problem of the data type...

Kind regards,
Silke.

On 22 Jan., 14:13, hadley wickham <h.wick...@gmail.com> wrote:

hadley wickham

unread,
Jan 22, 2009, 12:00:01 PM1/22/09
to silke, ggplot2
Ok, in that case you want the following in the body of your loop:

m <- m + geom_point(aes(x=x[,i],y=y[,i]))

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

silke

unread,
Feb 5, 2009, 10:19:00 AM2/5/09
to ggplot2
Hi,

I am sorry to bother again with this issue, but actually I avoided the
above loop problem by converting the data using as.numeric().
I don't know if I missed something in your answer above, but I didn't
get your solution to work, the result were (as above) 10 different
plots of each layer and not all 10 layers in one plot.
Now I need a similar plot with lines insted of points, and my
as.numeric solution does not work because I do not want all points to
be connected by a line, but:
The data points are in a 50x160 matrix, and I need each column of the
matrix to be one line (of length 50) in the plot which thus contains
160 single lines, plotted e.g. just against the numbers 1:50.
I also put the data into a list of length 160, with each list object
containing the vector I want to be one line, and tried to use melt (),
but I didn't get it to work neither.

I guess the solution must be quite simple but I'm stuck...

A simple example with a plot solution would be:
x = matrix(rnorm(50*160),ncol=160)
for (i in 1:160) x[,i]=x[,i]+i
plot(seq(1,50),x[,1],ylim=range(x),type="l")
for (i in 1:160) lines(seq(1,50),x[,i])

Can anybody help me finding a ggplot solution?

Many thanks in advance,
Silke.


On 22 Jan., 18:00, hadley wickham <h.wick...@gmail.com> wrote:
> Ok, in that case you want the following in the body of your loop:
>
> m <- m + geom_point(aes(x=x[,i],y=y[,i]))
>
> Hadley
>
>
>
> On Thu, Jan 22, 2009 at 10:04 AM, silke <silk...@gmail.com> wrote:
>
> > Thank you very much for your response, although the loop did not
> > really work for me as it returns 10 different plots, one of each
> > layer, and what I wanted is to have alllayersin the same plot.

baptiste auguie

unread,
Feb 5, 2009, 11:32:30 AM2/5/09
to silke, ggplot2
Hi,

If I understand correctly, you can try this:

y <- matrix(rnorm(50*20),ncol=20)

mdf <- data.frame(y)
mdf$x <- seq(1, nrow(y))

qplot(x, value, data=melt(mdf, id="x"), colour= variable , geom="line")

(I replaced 160 by 20 as it was quite slow on my machine)


note that in base graphics you could use matplot(1:50, y, lty=1)


Hope this helps,

baptiste


From: silke <sil...@gmail.com>
To: ggplot2 <ggp...@googlegroups.com>
Sent: Thursday, 5 February, 2009 15:19:00
Subject: Re: Problem with plotting two matrices and using a loop

silke

unread,
Feb 6, 2009, 8:56:22 AM2/6/09
to ggplot2
Yes, that helps, thank you very much!

On 5 Feb., 17:32, baptiste auguie <bapt_4...@yahoo.fr> wrote:
> Hi,
>
> If I understand correctly, you can try this:
>
> y <- matrix(rnorm(50*20),ncol=20)
>
> mdf <- data.frame(y)
> mdf$x <- seq(1, nrow(y))
>
> qplot(x, value, data=melt(mdf, id="x"), colour= variable , geom="line")
>
> (I replaced 160 by 20 as it was quite slow on my machine)
>
> note that in base graphics you could use matplot(1:50, y, lty=1)
>
> Hope this helps,
>
> baptiste
>
> ________________________________
> From: silke <silk...@gmail.com>
Reply all
Reply to author
Forward
0 new messages