R 如何在ggplot2中以对数比例显示stat_binhex

R 如何在ggplot2中以对数比例显示stat_binhex,r,ggplot2,hexagonal-tiles,R,Ggplot2,Hexagonal Tiles,我有一个有很多点的二维六边形密度图。我希望六边形内的计数以对数比例显示,但我不知道如何通过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() x当您未在stat\u binhex中指定时,有一个默认为的fill

我有一个有很多点的二维六边形密度图。我希望六边形内的计数以对数比例显示,但我不知道如何通过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()

x当您未在
stat\u binhex
中指定时,有一个默认为
fill
美学。下面的代码生成与原始代码相同的绘图

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

如果您想要拥有计数的对数刻度,那么解决方案非常简单:

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

另一种方法是将
trans=
参数传递给
scale\u 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")

这在ggplot2_2.1.0中不适用:eval(expr、envir、enclose)中出错:对象“count”不可用found@daknowles这很可能是由ggplot2中的错误引起的:在这个解决方案中,自动勾号选择和编号没有那么好,但在其他方面我很喜欢。trans=“log10”或trans=“log2”给出了更好的勾号