[R] plot matrix of characters

R. Michael Weylandt michael.weylandt at gmail.com
Wed Dec 14 18:20:12 CET 2011


Forgot to cc the list.

M

On Wed, Dec 14, 2011 at 12:19 PM, R. Michael Weylandt
<michael.weylandt at gmail.com> wrote:
> It's a bit of a hack-job, but this might get you started:
>
> d <- structure(c("A", "A", "A", "B", "A", "A", "C", "C", "A", "D",
> "C", "C"), .Dim = 3:4)
>
> plot(0, type = "n", xlim = c(0,ncol(d)), ylim = c(0, nrow(d)), xaxt = "n",
>     yaxt ="n",xlab = NA,ylab = NA)
>
> ytop <- (nrow(d) - row(d)) + 1
> ybottom <- (nrow(d) - row(d))
>
> xleft <- col(d)-1
> xright <- col(d)
>
> cols <- rainbow(length(unique(c(d))))
>
> rect(xleft = xleft, xright = xright, ytop = ytop, ybottom = ybottom,
>     col = cols[as.integer(factor(d))])
> text(xleft + 1/2, ybottom + 1/2, labels = d, col = "white")
>
>
> Some hints for a new poster:
> i) Always cc the list: it gives you opportunities for your questions
> to be seen by a much wider audience which will get you quicker and
> better answers.
> ii) Use dput() to make plain-text representations of your data for
> easy emailing (like how i set up d above)
>
> Best,
>
> Michael
>
> On Wed, Dec 14, 2011 at 11:53 AM, Ana <rrasterr at gmail.com> wrote:
>> What I would like to do is to plot the matrix A:
>>
>> "A" "B" "C" "D"
>> "A" "A" "C" "C"
>> "A" "A" "A" "C"
>>
>> into a image e.g.  with the following colours
>>
>> "Yellow" "Blue" "Green" "Black"
>> "Yellow" "Yellow" "Green" "Green"
>> "Yellow" "Yellow" "Yellow" "Green"
>>
>>
>> I have no idea on how to do it, until now I've been only working with
>> numeric and dates plots.
>>
>>
>>
>>
>> On Wed, Dec 14, 2011 at 5:35 PM, R. Michael Weylandt
>> <michael.weylandt at gmail.com> wrote:
>>> I may be confused, but this seems ill-defined since, in the example,
>>> the color corresponds to a numerical value: characters are only there
>>> as labels. Do you intend to map the characters onto some numerical
>>> spectrum? If so, how?
>>>
>>> As a side note, one of the best places to get visualization ideas in
>>> my experience has been the R Graph Gallery. It's often worth a look
>>> and comes with code examples for all the entries: perhaps you can find
>>> some attempts at "graphing" character data there.
>>>
>>> Michael
>>>
>>> On Wed, Dec 14, 2011 at 11:30 AM, Ana <rrasterr at gmail.com> wrote:
>>>> Hi, I am looking for options to plot the following type of matrices:
>>>>
>>>> "A" "B" "C" "D"
>>>> "A" "A" "C" "C"
>>>> "A" "A" "A" "C"
>>>>
>>>> as a image like this: http://www.phaget4.org/R/image002.jpg
>>>>
>>>> ______________________________________________
>>>> 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