r - 更改 grob 背景渐变的方向

标签 r ggplot2 background-image

我很惊讶以下从颜色向量创建的简单 grob 几乎按要求工作。

enter image description here

但是,我想让渐变从左到右,而不是从上到下。

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")))

enter image description here

关于r - 更改 grob 背景渐变的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48596582/

相关文章:

r - 多项式回归(不同的结果——相同的数据集,R vs SPSS)。 nnet 包 -- multinom 函数

r - R中的下划线图

r - 为 geom_bar 分配连续的填充颜色

background-image - Css 背景层和 100% 高度 div

Android:针对不同屏幕密度的背景图像大小的建议?

R StemCompletion 中的警告和 TermDocumentMatrix 中的错误

r - 由于意外的结果大小,函数停止运行

r - 为什么这个 R 代码没有产生相同的结果? (卷积与 FFT)

r - 从 ggplot 中提取数据

IOS 相当于 Android 选择器