geom_density and size by group

510 views
Skip to first unread message

Johannes Graumann

unread,
Jan 29, 2013, 7:14:47 AM1/29/13
to ggp...@googlegroups.com
Hi,

I'm trying to achieve a geom_density plot in which the size (line width) corresponds to the number of observations in the group represented.
Based on the help for stat_density, I am trying

> library(ggplot2)
> library(plyr) # to access round_any
> movies$decade <- round_any(movies$year, 10)
> m <- ggplot(movies, aes(x=rating, colour=decade, group=decade))
> m + geom_density(aes(fill=NA,size=..count..)

But this fails with
> Error in get(x, envir = this, inherits = inh)(this, ...) :
>  Aesthetics can not vary with a ribbon

Can anyone point out where I err and how to remedy?

Sincerely, Joh



Ben Bond-Lamberty

unread,
Jan 29, 2013, 7:20:03 AM1/29/13
to ggp...@googlegroups.com
The error message tells exactly what the problem is--you can't vary an
aesthetic (here line width) over a line; for the same reason, a line
can't change color as it goes. You'll need to find another way to plot
this information.

Ben
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Ista Zahn

unread,
Jan 29, 2013, 7:43:45 AM1/29/13
to Ben Bond-Lamberty, ggp...@googlegroups.com
On Tue, Jan 29, 2013 at 7:20 AM, Ben Bond-Lamberty <bpb...@gmail.com> wrote:
> The error message tells exactly what the problem is--you can't vary an
> aesthetic (here line width) over a line; for the same reason, a line
> can't change color as it goes. You'll need to find another way to plot
> this information.

That's not what the error says, nor is it true:

library(plyr) # to access round_any
movies$decade <- round_any(movies$year, 10)

m <- ggplot(movies, aes(x=rating))
m + stat_density(aes(color = ..count..), geom="line")

The error says aesthetics can't vary within a ribbon, but there is no
reason the density has to be plotted with a ribbon goem.

Best,
Ista

Ben Bond-Lamberty

unread,
Jan 29, 2013, 7:55:36 AM1/29/13
to Ista Zahn, ggp...@googlegroups.com
Whoops. Should have had a cup of coffee before posting. Thanks.

Johannes Graumann

unread,
Jan 29, 2013, 12:08:24 PM1/29/13
to ggp...@googlegroups.com, Ista Zahn
Thank you very much - I was at first puzzled why a ribbon would be used, but
looking at the fill examples, it makes sense now.

Anyhow, I got what I was aiming for now.

Sincerely, Joh
signature.asc
Reply all
Reply to author
Forward
0 new messages