3
votes

I really like the idea of a hexbin density plot in ggplot, and I try to use it (instead of a square-shaped bin as produced by stat_bin2d) whenever I can. However, the boundries of the hexagons are sometimes obvious. For example,

d <- ggplot(diamonds, aes(carat, price))

d + stat_binhex()

enter image description here

In this picture the boundaries of hexagons show up as little white lines, which sometimes interferes with my attempt to conceive the true "density" variations in the picture.

If I use stat_bin2d, the boundaries lines are not shown at all :

d <- ggplot(diamonds, aes(carat, price))

d + stat_bin2d()

enter image description here

So my questions are:

  1. Why the hexagon boundaries are displayed while square boundaries aren't.

  2. More importantly, is there a way to do stat_hexbin without showing boundary lines?

Thanks very much!

Aside: I prefer to do hexagon density plot in ggplot rather than using some other package mainly because I like the flexibility of adding other layers to it later on.

1
Possible workaround: you could just change the color of the outline to match the color of hexagons themselves, something like stat_binhex(color = 'royalblue4')ytk
Thanks Teja. It sounds plausible, but when I tried it the pic doesn't look very satisfactory...Ying Zhang

1 Answers

6
votes

Using the link to ggplot2 multiple stat_binhex() plots with different color gradients in one image as a reference I was able to do what you are asking with the following code:

d <- ggplot(diamonds, aes(carat, price))
d + stat_binhex(aes(colour = ..count..))

or

d <- ggplot(diamonds, aes(carat, price, colour = ..count..))
d + stat_binhex()

enter image description here