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 <- as.data.frame(cbind(x, y))
ggplot(df, aes(x, y)) + stat_binhex()
Another way is to pass trans =
argument to scale_fill_*
used.
ggplot(df, aes(x, y)) + geom_hex() + scale_fill_gradient(trans = "log")
ggplot(df, aes(x, y)) + geom_hex() + scale_fill_viridis_c(trans = "log")
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..))
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..)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With