我很惊讶以下从颜色向量创建的简单 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))
最佳答案
答案是 t
你必须转置你的grad
向量(输入到 rasterGrob
):
library(ggplot2)
ggplot() +
annotation_custom(rasterGrob(t(grad),
width = unit(1, "npc"), height = unit(1, "npc")))
关于r - 更改 grob 背景渐变的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48596582/