translating a list with matrix to dataframe for stat_contour

11 views
Skip to first unread message

Mark Connolly

unread,
Jun 7, 2010, 2:45:51 PM6/7/10
to ggplot2
I have a trend surface that is a list of x, y, and z. The x and y
define a coordinate grid. z is a matrix of values that fill the grid at
the intersection of the x and y.

Simply:
x = c(1:10)
y = c(1:10)
z = (x%o%y) # a matrix

ts = list(x,y,z) # a list with matrix
names(ts) = c("x","y","z")

This gives a structure that is great for contour, but not so good for
ggplot (as far as I can tell).

Given a structure such as ts, is there a direct way to change it into a
data frame that can be consumed by ggplot and stat_contour? All my
friends are laughing at my nested loop.

Thanks,

Mark

baptiste auguie

unread,
Jun 7, 2010, 3:12:32 PM6/7/10
to Mark Connolly, ggplot2
Hi,


something like this,

x = c(1:10)
y = c(1:100)


z = (x%o%y) # a matrix

d = with(melt(z), data.frame(x=x[X1],y=y[X2],z=value))

ggplot(d, aes(x, y, z = z)) + stat_contour()

HTH,

baptiste

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

Mark Connolly

unread,
Jun 7, 2010, 4:38:02 PM6/7/10
to baptiste auguie, ggplot2
Works beautifully! Many thanks Baptiste.

Mark

Reply all
Reply to author
Forward
0 new messages