[R] Define pch and color based on two different columns

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Wed Apr 10 07:20:08 CEST 2019


I believe this is unnecessarily complicated. What the OP did not seem to
get is that he had to specify the col's and pch's in the panel function
that were actually used via appropriate subscripting. He just passed in the
whole col and pch vector by default -- subscripting is not done unless
specified.So the following does what I think he wants very simply:

xyplot(mpg ~wt|cyl, data = mtcars,
      col = mtcars$gear,
      pch = mtcars$carb,
      panel = function(x,y, subscripts, col, pch,...)
      {
         panel.xyplot(x,y, col = col[subscripts], pch = pch[subscripts] )
      })

Cheers,
Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Tue, Apr 9, 2019 at 9:53 PM Peter Langfelder <peter.langfelder using gmail.com>
wrote:

> Sorry for being late to the party, but has anyone suggested a minor
> but important modification of the code from stack exchange?
>
> xyplot(mpg ~ wt | cyl,
>           panel = function(x, y, ..., groups, subscripts) {
>               pch <- mypch[factor(carb)[subscripts]]
>               col <- mycol[factor(gear)[subscripts]]
>               grp <- c(gear,carb)
>               panel.xyplot(x, y, pch = pch, col = col)
>           }
> )
>
> From the little I understand about what you're trying to do, this may
> just do the trick.
>
> Peter
>
> On Tue, Apr 9, 2019 at 2:43 PM Matthew Snyder <msnyder424 using gmail.com>
> wrote:
> >
> > I am making a lattice plot and I would like to use the value in one
> column
> > to define the pch and another column to define color of points. Something
> > like:
> >
> > xyplot(mpg ~ wt | cyl,
> >        data=mtcars,
> >        col = gear,
> >        pch = carb
> > )
> >
> > There are unique pch points in the second and third panels, but these
> > points are only unique within the plots, not among all the plots (as they
> > should be). You can see this if you use the following code:
> >
> > xyplot(mpg ~ wt | cyl,
> >        data=mtcars,
> >        groups = carb
> > )
> >
> > This plot looks great for one group, but if you try to invoke two groups
> > using c(gear, carb) I think it simply takes unique combinations of those
> > two variables and plots them as unique colors.
> >
> > Another solution given by a StackExchange user:
> >
> > mypch <- 1:6
> > mycol <- 1:3
> >
> > xyplot(mpg ~ wt | cyl,
> >           panel = function(x, y, ..., groups, subscripts) {
> >               pch <- mypch[factor(carb[subscripts])]
> >               col <- mycol[factor(gear[subscripts])]
> >               grp <- c(gear,carb)
> >               panel.xyplot(x, y, pch = pch, col = col)
> >           }
> > )
> >
> > This solution has the same problems as the code at the top. I think the
> > issue causing problems with both solutions is that not every value for
> each
> > group is present in each panel, and they are almost never in the same
> > order. I think R is just interpreting the appearance of unique values as
> a
> > signal to change to the next pch or color. My actual data file is very
> > large, and it's not possible to sort my way out of this mess. It would be
> > best if I could just use the value in two columns to actually define a
> > color or pch for each point on an entire plot. Is there a way to do this?
> >
> > Ps, I had to post this via email because the Nabble site kept sending me
> an
> > error message: "Message rejected by filter rule match"
> >
> > Thanks,
> > Matt
> >
> >
> >
> > *Matthew R. Snyder*
> > *~~~~~~~~~~~~~~~~~*
> > PhD Candidate
> > University Fellow
> > University of Toledo
> > Computational biologist, ecologist, and bioinformatician
> > Sponsored Guest Researcher at NOAA PMEL, Seattle, WA.
> > Matthew.Snyder6 using rockets.utoledo.edu
> > MSnyder424 using gmail.com
> >
> >
> >
> > [image: Mailtrack]
> > <
> https://mailtrack.io?utm_source=gmail&utm_medium=signature&utm_campaign=signaturevirality5&
> >
> > Sender
> > notified by
> > Mailtrack
> > <
> https://mailtrack.io?utm_source=gmail&utm_medium=signature&utm_campaign=signaturevirality5&
> >
> > 04/09/19,
> > 1:49:27 PM
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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.
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list