bin2d and hex mapped to other scale (alpha)

333 views
Skip to first unread message

Etienne Low-Decarie

unread,
Mar 19, 2013, 11:26:42 PM3/19/13
to ggp...@googlegroups.com
Is it possible to map the value of the bins from bind2d and hex to another scale than fill, notably alpha?  So that the bins with the largest counts are more opaque.

Thank you

Ista Zahn

unread,
Mar 20, 2013, 8:28:47 AM3/20/13
to Etienne Low-Decarie, ggp...@googlegroups.com
Yes, you can do this as follows:

d <- ggplot(diamonds, aes(carat, price))
d + geom_bin2d(aes(alpha=..count..))

Note that stat_bin2d does not document ..count.. as a return value (as
stat_bin does), so I discovered this by making the educated guess that
stat_bin2d works the same way as ?stat_bin. This really should be
documented.

Best,
Ista
> --
> --
> 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.
>
>
Message has been deleted

Etienne Low-Decarie

unread,
Mar 20, 2013, 8:34:57 AM3/20/13
to ggp...@googlegroups.com, Etienne Low-Decarie
Thank you!  That is great!  Do you know how to get it to map only to alpha (and not to fill)

This does not seem to work:
d <- ggplot(diamonds, aes(carat, price)) 
d + geom_bin2d(aes(alpha=..count..), fill=I("black")) 

Best regards,

Etienne

Kazimir Menzel

unread,
Mar 20, 2013, 8:59:45 AM3/20/13
to Etienne Low-Decarie, ggp...@googlegroups.com
It works if you include fill specification into the mapping:

d <- ggplot(diamonds, aes(carat, price)) 
d + geom_bin2d(aes(alpha=..count.., fill = NULL)) 

Good luck,

Kazimir.

Dennis Murphy

unread,
Mar 20, 2013, 11:28:18 AM3/20/13
to Etienne Low-Decarie, ggp...@googlegroups.com
Hi:

Generally speaking, the I() construct is specific to qplot(), but it
doesn't seem to cause any problems here when used with ggplot(). It's
not clear to me which of the two legends you want to remove (I would
guess the colorbar), but try these:

# library(scales)
# Gets rid of the grey scale legend:
d + geom_bin2d(aes(alpha = ..count..), fill = "black") +
scale_alpha(guide = "none")

# Gets rid of the fill colorbar:
d + geom_bin2d(aes(alpha = ..count..), fill = "black") +
scale_fill_continuous(guide = "none")

Dennis
Reply all
Reply to author
Forward
0 new messages