r - 在 grImport 的图片类中更改颜色

标签 r grimport

我找到了一个很棒的包 grImport 用于读取 .ps 文件(矢量与光栅)。它工作得很好,读者可以在这里找到包信息:

http://cran.r-project.org/web/packages/grImport/grImport.pdf

这里有一个演示:

https://www.stat.auckland.ac.nz/~paul/R/grImport/import.pdf

我希望能够更改 Picture 类对象中插槽的颜色,但不知道如何更改。所以使用这段代码:

library(grImport); library(grid)
## Create a generic .ps file to read in
postscript("foo.ps")
plot.new()
text(.5, 0.5, "A", cex = 45)
dev.off()  

## read in the .ps object
PostScriptTrace("foo.ps")
foo <- readPicture("foo.ps.xml")
grid.picture(foo)

如何更改对象 foo 以使 A 绘图为浅灰色,比如 #D0D0D0

我试过:

class(foo)
foo
foo@rgb

我认为这是一个 S4 对象,这可能就是我苦苦挣扎的原因(我对 s4 不熟悉)。

最佳答案

使用str探索S4对象的结构:

R> str(foo)
Formal class 'Picture' [package "grImport"] with 2 slots
  ..@ paths  :List of 1
  .. ..$ text:Formal class 'PictureText' [package "grImport"] with 14 slots
  .. .. .. ..@ string   : Named chr "A"
  .. .. .. .. ..- attr(*, "names")= chr "string"
  .. .. .. ..@ w        : num 3602
  .. .. .. ..@ h        : num 5400
  .. .. .. ..@ bbox     : num [1:4] 904 2644 4840 6154
  .. .. .. ..@ angle    : num 90
  .. .. .. ..@ letters  :List of 1

  ...

可以通过以下方式改变颜色:

foo@paths$text@letters$path@rgb <- "#D0D0D0"

关于r - 在 grImport 的图片类中更改颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23002825/

相关文章:

r - 将 lapply 转换为 foreach?

r - 在ggmap中使用图像作为点图标

python - 从共同作者数据创建边缘列表

r - 为什么 R 的 ifelse 语句不能返回向量?

r - 是否可以排除使用 sf 和 tmap 制作的 map 区域?

R:使用带有权重的 reshape2::acast 聚合