geom_ribbon and density

409 views
Skip to first unread message

Nolwenn Le Meur

unread,
Feb 9, 2011, 12:26:08 PM2/9/11
to ggplot2
Hi,

I'm trying to reproduce a lattice violin plot using ggplot2 as
exemplified on http://learnr.wordpress.com/ (code below) but I get the
following error message: "Non-continuous variable supplied to
scale_y_continuous."

I have noticed on the help pages that geom_ribbon() requires y range
with continuous x values. Therefore my question is how did learnr do
to get the right Figure?

Thank you
Nolwenn

## Lattice
pl <- bwplot(Days ~ log(FSC.H), gvhd10, panel = panel.violin,
box.ratio = 3, xlab = "log(Forward Scatter)", ylab = "Days Past
Transplant")

## ggplot2
p <- ggplot(gvhd10, aes(log(FSC.H), Days))
pg <- p + geom_ribbon(aes(ymax = ..density.., ymin = -..density..),
stat = "density") + facet_grid(Days ~ ., as.table = F, scales
="free_y") + labs(x = "log(Forward Scatter)", y = "Days Past
Transplant")
print(pg)

Brandon Hurr

unread,
Feb 9, 2011, 1:37:45 PM2/9/11
to Nolwenn Le Meur, ggplot2
> str(gvhd10)
'data.frame': 113896 obs. of  8 variables:
 $ FSC.H: num  548 213 205 119 474 198 267 60 69 552 ...
 $ SSC.H: num  536 33 38 45 904 45 177 82 8 544 ...
 $ FL1.H: num  1 2.13 4.11 1.55 170.86 ...
 $ FL2.H: num  20 104.1 141.4 630.4 2203.5 ...
 $ FL3.H: num  1 7.93 5.95 4.79 84.66 ...
 $ FL2.A: num  8 23 30 148 812 28 29 105 12 5 ...
 $ FL4.H: num  2.09 1 1 1.26 37.99 ...
 $ Days : Factor w/ 7 levels "-6","0","6","13",..: 1 1 1 1 1 1 1 1 1 1 ...

Days is a factor which is the problem so I tried forcing it to numeric and it seemed to work. 

ggplot(gvhd10, aes(log(FSC.H), as.numeric(Days))) + geom_ribbon(aes(ymax = ..density.., ymin = -..density..),
stat = "density") + facet_grid(Days ~ ., as.table = F,  scales
="free_y") + labs(x = "log(Forward Scatter)",  y = "Days Past
Transplant")

output attached... 

Brandon
as.numeric.png

Nolwenn Le Meur

unread,
Feb 9, 2011, 1:58:14 PM2/9/11
to ggplot2
Thank you Brandon. Works for me too (and more especially for my data)

I guess I had not tried everything :)

Nolwenn

On 9 fév, 19:37, Brandon Hurr <brandon.h...@gmail.com> wrote:
> > str(gvhd10)
>
> 'data.frame': 113896 obs. of  8 variables:
>  $ FSC.H: num  548 213 205 119 474 198 267 60 69 552 ...
>  $ SSC.H: num  536 33 38 45 904 45 177 82 8 544 ...
>  $ FL1.H: num  1 2.13 4.11 1.55 170.86 ...
>  $ FL2.H: num  20 104.1 141.4 630.4 2203.5 ...
>  $ FL3.H: num  1 7.93 5.95 4.79 84.66 ...
>  $ FL2.A: num  8 23 30 148 812 28 29 105 12 5 ...
>  $ FL4.H: num  2.09 1 1 1.26 37.99 ...
>  $ Days : Factor w/ 7 levels "-6","0","6","13",..: 1 1 1 1 1 1 1 1 1 1 ...
>
> Days is a factor which is the problem so I tried forcing it to numeric and
> it seemed to work.
>
> ggplot(gvhd10, aes(log(FSC.H), as.numeric(Days))) + geom_ribbon(aes(ymax =
> ..density.., ymin = -..density..),
> stat = "density") + facet_grid(Days ~ ., as.table = F,  scales
> ="free_y") + labs(x = "log(Forward Scatter)",  y = "Days Past
> Transplant")
>
> output attached...
>
> Brandon
>
> On Wed, Feb 9, 2011 at 17:26, Nolwenn Le Meur <nlem...@gmail.com> wrote:
>
> > ggplot(gvhd10, aes(log(FSC.H), Days))
> > pg <- p + geom_ribbon(aes(ymax = ..density.., ymin = -..density..),
> > stat = "density") + facet_grid(Days ~ ., as.table = F,  scales
> > ="free_y") + labs(x = "log(Forward Scatter)",  y = "Days Past
> > Transplant")
>
>
>
>  as.numeric.png
> 354KAfficherTélécharger

Dennis Murphy

unread,
Feb 9, 2011, 2:22:14 PM2/9/11
to Nolwenn Le Meur, ggplot2
Hi:

This works:

library(lattice)
library(latticeExtra)   # where the data frame resides
data(gvhd10)

ph <- ggplot(gvhd10, aes(x = log(FSC.H)))
ph + geom_ribbon(aes(ymax = ..density.., ymin = -..density..),

                          stat = "density") +
facet_grid(Days ~ ., as.table = FALSE,  scales = "free_y") +
xlab("log(Forward Scatter)")

You don't want Days as either an x or y aesthetic because you intend to use it as the grouping variable. The log(FSC.H) is the 'x' variable because it plays a role similar to that in geom_density() or geom_histogram() - it provides the domain for the estimated density to be computed.  ..density.. is the y-variable. The appearance has to do with the combination of the way that the plot is generated and the formula used for facet_grid().  [Think of a violin as a density plot reflected around its x-axis; this is why log(FSC.H) is the x variable.] To reproduce this structure by groups, Days has to be the 'y' variable in the facet_grid() formula so that each panel has ..density.. as its 'y'.

To do this in the other direction, we have to create a single violin plot as a separate object, using coord_flip() to switch the x and y axis,. We can then partition it by Days using facet_grid() again, except with Days on the RHS (x) this time. A couple of extra code chunks are added/modified to improve its appearance.

# Generate a violin plot and flip the x and y axes
pj <- ggplot(gvhd10, aes(x = log(FSC.H))) + 
      geom_ribbon(aes(ymax = ..density.., ymin = -..density..),
                          stat = "density") + coord_flip() +
      opts(axis.text.x = theme_text(angle = 90, hjust = 0.5)) +
      ylab("log(Forward Scatter)")
# Plot by groups with facet_grid()
pj + facet_grid(. ~ Days, scales = 'free_x' )

Hopefully, you can see the coordination that has to exist between the 'base' violin plot and facet_grid().

HTH,
Dennis


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

Brandon Hurr

unread,
Feb 9, 2011, 2:42:52 PM2/9/11
to Dennis Murphy, Nolwenn Le Meur, ggplot2
Dennis, 

Thanks for the explanation, Violin plots always just look like rorschach tests to me. (that one looks like a violin and that one's a cello)

One thing... 
pj + facet_grid(. ~ Days, scales = 'free_x' )

With 'free_x' the coord_flipped y axis doesn't line up, so you need to use 'free_y'... not freeing any scales also works well. 

Brandon

Dennis Murphy

unread,
Feb 9, 2011, 3:12:03 PM2/9/11
to Adam_L...@keybank.com, ggplot2
Hi:

On Wed, Feb 9, 2011 at 12:00 PM, <Adam_L...@keybank.com> wrote:

What does the "as.table" argument do basically, within the facet_grid?

Other than affecting the order in which the group variables appear, I'm having trouble seeing what difference it makes. Is that it's only purpose?

Yes. The idea was initially in lattice, which by default starts panels in the lower left corner, works to the right and then ascends in succeeding rows. After many complaints by people who wanted the panels ordered in the opposite direction,  Deepayan came up with the as.table argument in xyplot.  The use in ggplot2 emulates that idea. It tends to have more impact in 2D arrays of plots than in 1D, which may account for your perception of its usefulness :)

Dennis

Adam Loveland




Dennis Murphy <djm...@gmail.com>
Sent by: ggp...@googlegroups.com

02/09/2011 02:22 PM

To
Nolwenn Le Meur <nle...@gmail.com>
cc
ggplot2 <ggp...@googlegroups.com>
Subject
Re: geom_ribbon and density


This communication may contain privileged and/or confidential information. It is intended solely for the use of the addressee. If you are not the intended recipient, you are strictly prohibited from disclosing, copying, distributing or using any of this information. If you received this communication in error, please contact the sender immediately and destroy the material in its entirety, whether electronic or hard copy. This communication may contain nonpublic personal information about consumers subject to the restrictions of the Gramm-Leach-Bliley Act. You may not directly or indirectly reuse or redisclose such information for any purpose other than to provide the services for which you are receiving the information. 127 Public Square, Cleveland, OH 44114


If you prefer not to receive future e-mail offers for products or services from Key
send an e-mail to mailto:DNERe...@key.com with 'No Promotional E-mails' in the
SUBJECT line.

Adam_L...@keybank.com

unread,
Feb 9, 2011, 3:00:33 PM2/9/11
to Dennis Murphy, ggplot2

What does the "as.table" argument do basically, within the facet_grid?

Other than affecting the order in which the group variables appear, I'm having trouble seeing what difference it makes. Is that it's only purpose?

Adam Loveland




Dennis Murphy <djm...@gmail.com>
Sent by: ggp...@googlegroups.com

02/09/2011 02:22 PM

To
Nolwenn Le Meur <nle...@gmail.com>
cc
ggplot2 <ggp...@googlegroups.com>
Subject
Re: geom_ribbon and density







This communication may contain privileged and/or confidential information. It
is intended solely for the use of the addressee. If you are not the intended
recipient, you are strictly prohibited from disclosing, copying, distributing
or using any of this information. If you received this communication in error,
please contact the sender immediately and destroy the material in its entirety,
whether electronic or hard copy. This communication may contain nonpublic personal
information about consumers subject to the restrictions of the 
Gramm-Leach-Bliley Act. You may not directly or indirectly reuse or redisclose
such information for any purpose other than to provide the services for which
you are receiving the information.

127 Public Square, Cleveland, OH 44114

Reply all
Reply to author
Forward
0 new messages