ggplot2中的对数色标搜索某些图例编号

时间:2017-06-30 21:30:03

标签: r ggplot2 legend

我正在尝试创建一个类似于问题的回答:(Is there a built-in way to do a logarithmic color scale in ggplot2?)。

在上述链接中组合问题代码和接受的答案会创建以下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)  

这会产生以下图像:

My output

由于某种原因,图例的缩放看起来与上面链接中接受的答案的输出不同。在我创建的情节中,图例一起搜索某些数字(例如50,100,200),而链接上发布的图表似乎均匀分布了图例编号。下图显示了左侧帖子中创建的图例,以及右侧的图例。

Legend comparison

我喜欢链接中的情节中的传奇美学,并希望重新创建它。我有什么不同的想法吗?谢谢。

1 个答案:

答案 0 :(得分:1)

使用guide="legend"

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

查看?scale_fill_gradient

enter image description here