Here's an example of a binned density plot:
require(ggplot2)
n <- 1e5
df <- data.frame(x = rexp(n), y = rexp(n))
p <- ggplot(df, aes(x = x, y = y)) + stat_binhex()
print(p)
It would be nice to adjust the color scale so that the breaks are log-spaced, but a try
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))
Results in an Error: Continuous variable () supplied to discrete scale_hue.
It seems breaks is expecting a factor (maybe?) and designed with categorical variables in mind?
There's a not built-in work-around I'll post as an answer, but I think I might just be lost in my use of scale_fill_hue
, and I'd like to know if there's anything obvious I'm missing.
Another way, using a custom function in
stat_summary_hex
:This is now part of
ggplot
, but was originally inspired by the wonderful code by by @kohske in this answer, which provided a customstat_aggrhex
. In versions of ggplot > 2.0, use the above code (or the other answer)To generate this plot.
Yes! There is a
trans
argument toscale_fill_gradient
, which I had missed before. With that we can get a solution with appropriate legend and color scale, and nice concise syntax. Usingp
from the question andmy_breaks = c(2, 10, 50, 250, 1250, 6000)
: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
.