user3389288 user3389288 - 6 months ago 34
R Question

How to make stat_binhex shown on a log scale in ggplot2

I have a 2d hexagon density plot with many points. I would like the counts within the hexagons to be displayed on a logarithmic scale, but I can't figure out how to do this through ggplot2.

Here is a simple example:

x <- runif(1000, 50, 100)
y <- rnorm(1000, mean = 10, sd = 8)

df <-, y))

ggplot(df, aes(x, y)) + stat_binhex()

Answer Source

There is a fill aesthetics that defaults to ..count.. when you do not specify it in stat_binhex. The code below produces same plot as your original code.

ggplot(df, aes(x, y)) + stat_binhex(aes(fill=..count..))

enter image description here

If you want to have a log scale for counts, then the solution is pretty straight forward:

ggplot(df, aes(x, y)) + stat_binhex(aes(fill=log(..count..)))

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download