简体   繁体   中英

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 <- as.data.frame(cbind(x, y))

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

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

在此输入图像描述

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

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM