【问题标题】:Transfor a Gif to a grob in R to use with annotation_custom in a ggplot将 Gif 转换为 R 中的 grob 以在 ggplot 中与 annotation_custom 一起使用
【发布时间】:2017-04-25 04:50:11
【问题描述】:

我正在尝试导入 gif 并将其转换为 grob,以便在 ggplot 中将其与函数 annotation_custom 一起使用,到目前为止,我已将其导入并能够将其转换为图像。

到目前为止,这是我所拥有的:

library(caTools)
require(ggplot2)
require(grid)
require(gridExtra)
y <- read.gif("http://blog.helpingadvisors.com/wp-content/uploads/2011/01/basketballcourt.gif")

我得到一个列表,到目前为止我已经能够绘制它:

image(y$image, col=y$col, main=y$comment, asp=1)

但我已经尝试将其转换为 grob:

a <- rasterGrob(image(y$image, col=y$col, main=y$comment, asp=1))

或者试试

raster(image(y$image, col=y$col, main=y$comment, asp=1))

然后将其保存为 grob,但由于图像返回类 NULL,它不起作用。

我也尝试使用 y 中的矩阵,但它的颜色非常奇怪:

plot(raster(y$image))

我真的需要原来的颜色

【问题讨论】:

    标签: r ggplot2 gif grob


    【解决方案1】:
    mat = y$col[y$image+1]
    dim(mat) = dim(y$image)
    qplot(1,1) + annotation_custom(rasterGrob(mat))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-16
      • 2022-01-13
      • 2021-08-19
      相关资源
      最近更新 更多