geom_hex and fill

956 views
Skip to first unread message

Claudia Beleites

unread,
Sep 1, 2010, 3:20:23 PM9/1/10
to ggp...@googlegroups.com
Dear all,

I want to produce a plot of hexagons, filled with a colour that I calculate
externally.
I easily obtain a data.frame with the centres of the hexagons and the desired
colours.

Everything goes fine until I try filling with a discrete variable. (Obviously at
the end there's to be a scale_fill_identity, but that doesn't change anything).
Playing around with aes (colour = col) seems to have the same effect.

What am I missing?

Best regards,

Claudia


library (ggplot2)
library (hexbin)


df <- data.frame (x = 1 : 3,
y = (1:3)*sqrt (3),
col = c ("#008000", "#0000A0", "#A00000"))

ggplot (data = df, aes (x = x, y = y)) + geom_hex (colour = "red", stat =
StatIdentity) +
scale_x_continuous (expand = 0 : 1) + scale_y_continuous (expand = c (0, 2))
+ coord_equal ()
## fine

ggplot (data = df, aes (x = x, y = y, fill = x)) + geom_hex (colour = "red",
stat = StatIdentity) +
scale_x_continuous (expand = 0 : 1) + scale_y_continuous (expand = c (0, 2))
+ coord_equal ()
## fine

ggplot (data = df, aes (x = x, y = y, fill = col)) + geom_hex (colour = "red",
stat = StatIdentity) +
scale_x_continuous (expand = 0 : 1) + scale_y_continuous (expand = c (0, 2))
+ coord_equal ()
## size badly screwed up: far too large


--
Claudia Beleites
Dipartimento dei Materiali e delle Risorse Naturali
Università degli Studi di Trieste
Via Alfonso Valerio 6/a
I-34127 Trieste

phone: +39 0 40 5 58-37 68
email: cbel...@units.it

Hadley Wickham

unread,
Sep 2, 2010, 2:30:03 PM9/2/10
to Claudia Beleites, ggp...@googlegroups.com
Hi Claudia,

Try this:

ggplot (data = df, aes (x = x, y = y, fill = col, group = 1)) +


geom_hex (colour = "red", stat = StatIdentity) +
scale_x_continuous (expand = 0 : 1) +
scale_y_continuous (expand = c (0, 2)) +
coord_equal ()

The problem is that the hexagons get broken up automatically by a
discrete fill colour, and the automatic estimation of their size goes
wrong.

Hadley

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

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Claudia Beleites

unread,
Sep 4, 2010, 6:54:11 AM9/4/10
to Hadley Wickham, ggp...@googlegroups.com
Dear Hadley,

many thanks, that works beautifully.

I promise an example for the Wiki :-)

Claudia


--
Claudia Beleites
Dipartimento dei Materiali e delle Risorse Naturali

Universit� degli Studi di Trieste

Reply all
Reply to author
Forward
0 new messages