[R] Plotting symbols and colors based upon data values
Peter Ehlers
ehlers at ucalgary.ca
Mon Mar 14 13:16:41 CET 2011
On 2011-03-13 18:46, David Winsemius wrote:
>
> On Mar 13, 2011, at 8:51 PM, Mark Linderman wrote:
>
>> 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)
>
> I get the same problem and after experimenting for a while I think I
> can solve it by randomizing the order of the entries:
>
> > broken<- broken[sample(417), ]
>
> > xyplot(Y ~ X | A, data=broken, pch=broken$C, col=broken$B)
>
> Why xyplot should fail to properly assign pch values just because all
> "1"'s are at the beginning seems to me to be a bug.
The pch/col argument will just cycle through its elements
successively for each point plotted, beginning again with the
first element for each panel. So, if the first 92 elements (the
largest group in broken$A has 92 cases) of broken$C are all 1s,
then you'll never see any other symbol in any of the panels. Note
also that none of the panels show any 'blue' points, since there
are no "blue"s in the first 92 elements of broken$B. That this
seems to be 'fixed' by randomizing the rows of the data frame is
an illusion. The colours/symbols aren't correctly associated with
the points.
The way to get the desired effect is to use col/pch in the
panel function:
broken$B <- as.character(broken$B)
xyplot(Y ~ X | A, data = broken,
panel = function(x=X, y=Y, subscripts){
panel.xyplot(x, y,
col = broken$B[subscripts],
pch = broken$C[subscripts])
})
where 'subscripts' pulls out the appropriate parts of the
data frame according to the panel being plotted and the
colour vector should be of type 'character'.
Peter Ehlers
>
> --
> David.
>
> After confirming the the problem recurs when re-order()-ed by broken
> $C, I am appending dput( ordered-broken) for others to experiment
>
> > dput(broken[order(broken$C), ])
> structure(list(rown = c(91L, 193L, 128L, 8L, 143L, 46L, 60L,
> 99L, 112L, 67L, 25L, 15L, 188L, 93L, 115L, 4L, 190L, 64L, 147L,
<snip>
More information about the R-help
mailing list