Ying Zhang
Ying Zhang

Reputation: 181

How to get rid of boundary lines in ggplot geom_hex

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.

Upvotes: 4

Views: 2469

Answers (1)

steveb
steveb

Reputation: 5532

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

Upvotes: 6

Related Questions