[R] Plotting symbols and colors based upon data values

Mark Linderman mark.linderman at gmail.com
Mon Mar 14 01:51:49 CET 2011


David, thank you for your quick reply.  I spent a few minutes getting your
command to work with some sparse synthetic data, and then spent several
hours trying to figure out why my data didn't work (at least for symbols,
colors look okay).  I have massaged my data to where it is practically
indistinguishable from the synthetic data - yet it still doesn't work.
Attached are the two data files that can be plotted as follows:

broken = read.table("broken.table",header=TRUE)
works = read.table("works.table",header=TRUE)
xyplot(Y ~ X | A, data=works, pch=works$C , col=works$B)
xyplot(Y ~ X | A, data=broken, pch=broken$C , col=broken$B)

Only difference I see is that my data is largely sorted by $C whereas the
working data frame is not.   Not sure why that would make a difference.

Thanks again for your help!
Mark


> head(broken)
          X         Y     A     B C
1 0.3476158 0.5334874 Cat A   red 1
2 0.5692598 0.3205288 Cat A   red 1
3 0.5879649 0.3593725 Cat A black 1
4 0.9642691 0.9242240 Cat A black 1
5 0.5303471 0.7964391 Cat A   red 1
6 0.9998770 0.1722618 Cat A black 1

> head(works)
           X  Y     A      B C
1 0.55722499 31 cat D yellow 2
2 0.75100600 32 cat B    red 5
3 0.21665005 33 cat C  green 4
4 0.01201102 34 cat B    red 3
5 0.78503588 35 cat B  black 2
6 0.53589896 36 cat D   blue 5
-----Original Message-----
From: David Winsemius [mailto:dwinsemius at comcast.net] 
Sent: Saturday, March 12, 2011 10:39 PM
To: Mark Linderman
Cc: r-help at r-project.org
Subject: Re: [R] Plotting symbols and colors based upon data values


On Mar 12, 2011, at 7:57 PM, Mark Linderman wrote:

> I am new to R and am sure this is simple, but  I been unable to find a 
> solution.
>
> I have 5 columns of data labeled "X", "Y", "A","B","C".  I can easily 
> xyplot(Y ~ X | A) but I want the colors of the symbols to be based 
> upon the
> values of B and the shape of the symbols to be determined by C.   
> There are
> approximately four distinct values of B and C (say "b1","b2","b3","b4" 
> and "c1","c2","c3","c4", respectively)
>
No data to check it against (despite the request for such that accompanies
every posting) but see if this give the desired result:

xyplot(Y ~ X | A, data=dfrm2, pch=dfrm2$C , col=dfrm2$B)


> Either a solution or a pointer to a specific reference/example is 
> greatly appreciated.

There are many in the contributed documentation as well as in Sarkar's book
website and in the graphics galleries. As you suggested, it's pretty basic
stuff since you are benefiting from Sarkar's effort to carry over some of
the argument names from basic graphics. The one "trick" is to not rely on
the argument being assumed to come from the environment of the `data`
argument.

-- 

David Winsemius, MD
West Hartford, CT


More information about the R-help mailing list