在下图中,a 的范围从 0.001 到 0.7,但图例中没有青色的中间值。有没有办法可以强制 a 范围内的中间值位于色标的中间?
a <- c(0.1, 0.05, 0.09, 0.001, 0.7, 0.002, 0.1, 0.015, 0.05, 0.013, 0.05, 0.0021, 0.05, 0.002, 0.5, 0.01, 0.2, 0.001)
b <- c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9)
depth <- seq(1:18)
df <- data.frame(a, b, depth)
colors <- c("darkblue", "blue", "cyan", "yellow", "red")
range(df$a)
> range(df$a)
[1] 0.001 0.700
ggplot(df, aes(x = b, y = depth, fill = a)) +
geom_tile(width = 1, height = 1) +
scale_y_reverse(name = "Depth (m)") +
scale_fill_gradientn(colors = colors, limits = c(0.01, 0.3), oob = scales::squish) +
labs(x = "Category", y = "Depth (m)", fill = "Value") +
theme_minimal()
手动设置刻度限制时...
## ggplot code +
scale_fill_gradientn( ..., limits = c(.01, 0.3)))
...确保它们符合颜色映射数据的范围(此处:
c(.001, .7)
,或者完全保留手动规范,在这种情况下,ggplot
会处理它。
作品:
## ggplot code +
scale_fill_gradientn(colors = colors,
limits = c(0.001, 0.07) # match value range or skip
oob = scales::squish
)