您可以使用
values=
参数指定如何重新缩放数据,即根据文档:
如果颜色不应沿梯度均匀分布,则该向量给出了颜色向量中每种颜色的位置(在0和1之间)。
library(ggplot2)
set.seed(123)
breaks <- c(0, 0.25, 0.5, 1, 2, 5, 10, 20)
ggplot(df) +
geom_tile(aes(x = x, y = y, fill = z)) +
scale_fill_stepsn(
colors = terrain.colors(7),
breaks = breaks,
values = scales::rescale(breaks),
limits = range(breaks)
)