Logarithmic color scale in ggplot2 squishes certai

2019-08-10 23:20发布

问题:

I am trying to create a plot similar to what was asked and answered at: (Is there a built-in way to do a logarithmic color scale in ggplot2?).

Combining the code from the question and accepted answer at the above link creates the following MWE:

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

my_breaks = c(2, 10, 50, 100, 200, 6000)
p + scale_fill_gradient(name = "count", trans = "log", breaks = my_breaks, labels = my_breaks)  

This leads to the following image:

For some reason, the scaling of the legend looks different than the output from the accepted answer at the above-posted link. In the plot I created, the legend squishes certain numbers together (eg. 50, 100, 200), whereas the plot posted on the link seems to evenly distribute the legend numbers. The image below shows the legend created in the post on the left, and my legend on the right.

I like the aesthetics of the legend from the plot from the link, and want to recreate it. Any ideas what I may be doing differently? Thank you.

回答1:

Use guide="legend"

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

Check out ?scale_fill_gradient



标签: r ggplot2 legend