Dataframe Organization for Facetting

7 views
Skip to first unread message

Christian Lindig

unread,
Feb 20, 2009, 5:14:30 AM2/20/09
to ggplot2

I'd like to ask for advice how to organize a data frame to benefit
from facetting in ggplot2. I have data from a user survey in an SQL
database which I'd like to import into a dataframe and analyze using
ggplot2. One record of survey data consists of (say) 10 numbers, each
an answer to a question q1 .. q10.

At the database level q1 up to q10 are columns such that each row
(record) consists of an ID and 10 columns. What would be the best way
to organize the dataframe for R? "The R Book" suggests to have all
responses in one column and to label each one using a factor.
Unfortunately, I find this difficult to realize during an import. What
do you recommend?

-- Christian


David Hajage

unread,
Feb 20, 2009, 5:39:12 AM2/20/09
to Christian Lindig, ggplot2
It is difficult during an import, but it is very easy to do after import with reshape package and melt function. For example :
df <- data.frame(id = 1:10, q1 = rnorm(10), q2 = rnorm(10), q3 = rnorm(10))
df
melt(df)

David

Christian Lindig

unread,
Feb 20, 2009, 10:55:28 AM2/20/09
to ggplot2


On Feb 20, 11:39 am, David Hajage <dhaj...@gmail.com> wrote:
> It is difficult during an import, but it is very easy to do after import
> with reshape package and melt function. For example :
> df <- data.frame(id = 1:10, q1 = rnorm(10), q2 = rnorm(10), q3 = rnorm(10))
> df
> melt(df)

I just learned: melt() unleashes the power of ggplot2 because it
transforms a dataframe into a format that enables faceting. Thanks for
the example!


Reply all
Reply to author
Forward
0 new messages