[R] From two colors to 01 sequences

Paul Smith phhs80 at gmail.com
Tue May 12 14:41:40 CEST 2009


Thanks, Baptiste and Zeljko. I am attaching here an example of the
picture of the rectangles.

Paul


On Tue, May 12, 2009 at 1:23 PM, baptiste auguie <ba208 at exeter.ac.uk> wrote:
>
> Depending on the nature of your pdf file, it may be possible to use
> the grImport package. I've never used it before, but a quick test
> seems promising,
>
>>  # create a test picture
>> colorStrip <-
>> function (colors, draw = T)
>> {
>>     x <- seq(0, 1 - 1/ncol(colors), length = ncol(colors))
>>     y <- rep(0.5, length(colors))
>>     my.grob <- grid.rect(x = unit(x, "npc"), y = unit(y, "npc"),
>>         width = unit(1/ncol(colors), "npc"), height = unit(1,
>>             "npc"), just = "left", hjust = NULL, vjust = NULL,
>>         default.units = "npc", name = NULL, gp = gpar(fill =
>> rgb(colors[1,
>>             ], colors[2, ], colors[3, ]), col = rgb(colors[1,
>>             ], colors[2, ], colors[3, ])), draw = draw, vp = NULL)
>>     my.grob
>> }
>>
>>
>> colors <- rbind(c(1, 0, 1), c(0, 1, 0), c(0, 0, 0))
>>
>> pdf("testRGB.pdf")
>> colorStrip(colors)
>> dev.off()
>>
>>
>> # import the pdf file into R
>> library(grImport)
>> PostScriptTrace("testRGB.pdf")
>>
>> test <- readLines("testRGB.pdf.xml")
>>
>> testRead <- readPicture("testRGB.pdf.xml")
>>
>> str(testRead)
>> grid.picture(testRead) # somehow I've lost the fill color in the
>> process?!
>>
>> grep("<rgb.+", test, value=T) # this should allow you to find the
>> sequence of red and green rectangles
>
> HTH,
>
> baptiste
>
>
>
>
> On 12 May 2009, at 13:38, Zeljko Vrba wrote:
>>>
>>> I have got several pdf files with rows of colored rectangles: red
>>> rectangles should be read as 0; green rectangles as 1. No other color
>>> exists. Is there some way to have R reading the colored rectangles to
>>> a matrix or data frame converting the color of the rectangles to
>>> sequences of 01?
>>>
>
> _____________________________
>
> Baptiste Auguié
>
> School of Physics
> University of Exeter
> Stocker Road,
> Exeter, Devon,
> EX4 4QL, UK
>
> Phone: +44 1392 264187
>
> http://newton.ex.ac.uk/research/emag
> ______________________________
>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>


More information about the R-help mailing list