Die*_*nne 4 r background-image ggplot2
令我感到惊讶的是,从颜色矢量创建的以下简单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))
Run Code Online (Sandbox Code Playgroud)
答案是 t
你必须转置你的grad矢量(输入到rasterGrob):
library(ggplot2)
ggplot() +
annotation_custom(rasterGrob(t(grad),
width = unit(1, "npc"), height = unit(1, "npc")))
Run Code Online (Sandbox Code Playgroud)