令我惊讶的是,下面这个从颜色矢量创建的简单grob几乎可以按要求工作。

但是,我想让渐变从左到右,而不是从上到下。
library(ggplot2)
library(grid)
grad = colorRampPalette(c("red", "yellow"))(10)
ggplot(df, aes(x,y)) +
annotation_custom(rasterGrob(grad,
width=unit(1,"npc"),
height=unit(1,"npc"))) +
scale_x_continuous(limits = c(0,1)) +
scale_y_continuous(limits = c(0,1))发布于 2018-02-03 19:19:18
答案是t
您必须转置您的grad向量(输入到rasterGrob):
library(ggplot2)
ggplot() +
annotation_custom(rasterGrob(t(grad),
width = unit(1, "npc"), height = unit(1, "npc")))

https://stackoverflow.com/questions/48596582
复制相似问题