如何在ggplot2中以对数刻度显示stat_binhex

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)

Art*_*sov 8

有一种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)

在此输入图像描述

  • 这在ggplot2_2.1.0中对我不起作用:eval中的错误(expr,envir,enclos):找不到对象'count' (2认同)

小智 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)

在此处输入图片说明

  • trans="log10" 或 trans="log2" 提供更好的刻度线 (4认同)