rgrimport

Alter color in Picture class in grImport


I found a terrific package grImport for reading in .ps files (vector vs. raster). It works very well and the reader can find the package info here:

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

and a demo here:

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

I'd like to be able to change the color of slots in an object of the class Picture but can not figure out how. So using this code:

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)

How can I alter the object foo to make the A plot as a light grey, say #D0D0D0?

I tried:

class(foo)
foo
foo@rgb

I assume this is an S4 object which may be why I am struggling (I lack familiarity with s4).


Solution

  • Use str to explore the structure of the S4 object:

    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
    
      ...
    

    The color can be changed in the following way:

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