在 ggplot2中是否有一种内置的方法来做对数颜色刻度?

下面是一个装箱密度图的例子:

library(ggplot2)
n <- 1e5
df <- data.frame(x = rexp(n), y = rexp(n))
p <- ggplot(df, aes(x = x, y = y)) + stat_binhex()
print(p)

enter image description here

如果能够调整颜色比例,以便中断是对数间隔的,那就再好不过了,不过这是一次尝试

my_breaks <- round_any(exp(seq(log(10), log(5000), length = 5)), 10)
p + scale_fill_hue(breaks = as.factor(my_breaks), labels = as.character(my_breaks))

结果在一个 Error: Continuous variable () supplied to discrete scale_hue.它似乎打破是期待一个因素(也许?)和设计的范畴变量在心目中?

这里有一个不是内置的工作环境,我将张贴作为一个答案,但我认为我可能只是迷失在我的使用 scale_fill_hue,我想知道是否有什么明显的东西,我错过了。

39895 次浏览

Another way, using a custom function in stat_summary_hex:

ggplot(cbind(df, z = 1), aes(x = x, y = y, z = z)) +
stat_summary_hex(function(z){log(sum(z))})

This is now part of ggplot, but was originally inspired by the wonderful code by by @kohske in this answer, which provided a custom stat_aggrhex. In versions of ggplot > 2.0, use the above code (or the other answer)

ggplot(cbind(df, z = 1), aes(x = x, y = y, z = z)) +
stat_aggrhex(fun = function(z) log(sum(z))) +
labs(fill = "Log counts")

To generate this plot.

enter image description here

Yes! There is a trans argument to scale_fill_gradient, which I had missed before. With that we can get a solution with appropriate legend and color scale, and nice concise syntax. Using p from the question and my_breaks = c(2, 10, 50, 250, 1250, 6000):

p + scale_fill_gradient(name = "count", trans = "log",
breaks = my_breaks, labels = my_breaks)

enter image description here

My other answer is best used for more complicated functions of the data. Hadley's comment encouraged me to find this answer in the examples at the bottom of ?scale_gradient.