Logarithmic color scale in ggplot2 squishes certai

2019-08-10 23:16发布

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:

My output

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.

Legend comparison

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.

标签: r ggplot2 legend
1条回答
ゆ 、 Hurt°
2楼-- · 2019-08-10 23:53

Use guide="legend"

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

Check out ?scale_fill_gradient

enter image description here

查看更多
登录 后发表回答