use*_*288 6 r hexagonal-tiles ggplot2
我有一个二维六边形密度图,有许多点.我希望六边形内的计数以对数刻度显示,但我无法通过ggplot2弄清楚如何做到这一点.
这是一个简单的例子:
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()
Run Code Online (Sandbox Code Playgroud)
有一种fill美学默认为..count..,当你不指定它stat_binhex.下面的代码生成与原始代码相同的图.
ggplot(df, aes(x, y)) + stat_binhex(aes(fill=..count..))
Run Code Online (Sandbox Code Playgroud)

如果您想要计数的对数比例,那么解决方案非常简单:
ggplot(df, aes(x, y)) + stat_binhex(aes(fill=log(..count..)))
Run Code Online (Sandbox Code Playgroud)

小智 6
另一种方法是将trans =参数传递给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")
Run Code Online (Sandbox Code Playgroud)