通过在 scale_fill_gradientn (ggplot2) 中设置的颜色和值中断更新连续颜色条中的刻度

问题描述 投票:0回答:0

这是我使用 ggplot 绘制的可重现代码:

set.seed(123)
library(ggplot2)
df = data.frame(
    "x_var" = factor(rep(letters[1:10], 100)),
    "y_var" = factor(rep(LETTERS[1:10], each=100)),
    "fill_var" = sample(c(rexp(200, rate = 100),rexp(600, rate = 10), rexp(100, rate = 1), rexp(100, rate = 0.01)))
)

fill_colours = c("chartreuse4", "chartreuse3", "darkolivegreen2",
                 "khaki", "gold", "darkorange", "firebrick", "darkred")
fill_values_quantiles = seq(from = 0.05,
                            to = 0.95,
                            length.out = length(fill_colours) - 2)

ggplot(data = df, aes(x = x_var, y = y_var, fill = fill_var)) +
    geom_tile() +
    scale_fill_gradientn(
        colours = fill_colours,
        values = c(0,
                             quantile(df$fill_var, fill_values_quantiles),
                             ceiling(max(df$fill_var)))
        )

这给出了这个数字:

tile plot with quantiles

如何获得这样的图例栏?

enter image description here

我用这种颜色渐变绘制了这个图,因为使用分位数来突出显示图块图中的差异符合我想要显示的内容。该图将使用不同的数据帧进行多次。

我想要像我上面举的例子那样的图例,这样我们就可以更好地了解绿色值的情况。用文字来说,我想要与我在

values
的参数
scale_fill_gradientn
中输入的值相对应的刻度,并且我希望它们均匀分布。我仍然想要一个连续的颜色条。

Another idea instead of using quantiles:我也可以使用某种转换,例如对数转换,但在那种情况下,我希望

fill_val
的实际值打印在图例文本中的刻度处,而不是转换后的值。但我更喜欢另一种可能性。

r ggplot2 legend gradient geom-tile
© www.soinside.com 2019 - 2024. All rights reserved.