scatterplot from molten? dataframe

24 views
Skip to first unread message

jacktanner

unread,
Sep 7, 2009, 3:16:40 PM9/7/09
to ggplot2
Apologies for the noob question. This has probably been answered over
and over, but I can't think of the right keywords to search for.

I want to make a scatterplot using some data from a database query. It
comes like this:

> res = dbGetQuery()
> res
prop val fctr
x 0.1 a
y 0.3 a
x 0.8 b
y 1.3 b

prop is the variable to be plotted (on the x or the y axis,
respectively); val is the variable's value; fctr is a factor with a
couple of levels.

I know I can do this:

qplot(subset(res, prop=="x")$val,
subset(res, prop=="y")$val,
geom="jitter")

But this doesn't let me do a qplot(data=res, colour=fctr), because x
and y are two independent variables, i.e., I ignore the fact that
they're coming from the same dataframe res. I think that one solution
is to reshape the dataframe, but that seems like a kluge.

Thanks in advance.

hadley wickham

unread,
Sep 7, 2009, 10:26:37 PM9/7/09
to jacktanner, ggplot2
Hi Jack,

Could you give a bit more information about how the rows are lined up?
Is there's some implicit id in there somewhere? Could the following
ever happen?

prop val fct
x 0.1 a
y 0.3 b

i.e. inconsistent factor? If not, why not?

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

jacktanner

unread,
Sep 8, 2009, 8:53:33 AM9/8/09
to ggplot2
Hadley, thanks for thinking about this. The dataframe. can be made to
have four cols:

id prop fct val
1 x a 0.1
1 y b 0.3

id is a person, prop is the measurement of the person's w.r.t. the
factor fct. Suppose the cross-product prop X fct yields 6 combinations
(prop can be x or y, fct can be a, b, or c). Then every id (person) is
guaranteed to have been measured w.r.t each of these 6 combinations
exactly once.

In other words, every point (x,y) in the scatterplot is a person being
measured on x and on y w.r.t. to some factor fct, and every person
should have exactly 3 such points because there are 3 factors.

jacktanner

unread,
Sep 10, 2009, 1:32:18 PM9/10/09
to ggplot2
Hi everyone, I'm still struggling with this. Thanks in advance for
your help.

Please ignore the earlier posts regarding this; here's a concise
summary. What I want is a scatterplot where every point (x,y) is a
person being measured on x and on y w.r.t. to some factor, and every
person should have exactly 3 such points because there are 3 factors.
I want to color the points according to the factor.

And here's the dataframe I get from the database:

id prop fct val
1 x a 0.1
1 y a 0.3
1 x b 0.2
1 y b 0.4
1 x c 0.3
1 y c 0.6

This is person w/ id=1 measured on x and y w.r.t. factors a, b, and c,
which should result in exactly 3 points.

jacktanner

unread,
Sep 10, 2009, 1:51:26 PM9/10/09
to ggplot2, juliet...@gmail.com
Here's the same data in CSV format, so it can be read in with read.csv
()

id,prop,fct,val
1,x,a,0.1
1,y,a,0.3
1,x,b,0.2
1,y,b,0.4
1,x,c,0.3
1,y,c,0.6

Charlotte Wickham

unread,
Sep 10, 2009, 2:45:14 PM9/10/09
to jacktanner, ggplot2, juliet...@gmail.com
I think reshaping is the only way here. Assuming your data is in a
dataframe called res:

qplot(x, y, data = cast(res, id + fct ~ prop), colour = fct)

Charlotte
Reply all
Reply to author
Forward
0 new messages