1

I have a lattice plot of experimental data that has 4 datasets (i.e. 4 different subsets in "Config." below), each dataset subdivided according to the config and then also grouped within each test type according to wind direction, "Dir". I am able to set the shape of the points according to the grouping "Dir" across all plots but i cannot then set colours according to "Config" so that for example the first plot has all red points, the second plot has all blue points etc. I am using an xyplot in lattice and the code for the plot is:

xyplot(ach[,"F"]~ach[,"Ar_sqrt"] | Config., type=c("p"),ach, groups=Dir, 
   auto.key=list(TRUE, space="bottom", columns=4, padding.text=(8)), 
   Outer=TRUE, allow.multiple=TRUE, as.table=TRUE, scales=list(tick.number=10),
   grid=TRUE, par.settings=simpleTheme(pch=points$shape, cex=1.75, col=1),
   abline=list(c(0,0.2013), col=2, type="l"), layout=c(1,4),  
   panel = function( x,y,...) {
     panel.abline(lm(y~x),col = "black", lwd=1)
     panel.xyplot( x,y,...)
   })

I cant attach an image as my rep is les sthan 10, apologies. I have come across different ways of setting colours, etc but they all seem to either use the grouping function.

the data set is available here, (i added columns for pch and col to try and use this but to no avail:

https://www.dropbox.com/sh/ug9kun9rycsb1fw/AABSXuMs9kEWSgeEAcTYhDkxa

any help or direction to duplicates i didnt find very much appreciated...

vrajs5
  • 4,066
  • 1
  • 27
  • 44
  • 1
    Can you make a reproducible example that is not dependent on your link on Dropbox? See http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for some hints on how to do that. – Roman Luštrik Jun 18 '14 at 11:08
  • please provide sample of your data... You can use `dput(head(ach,20))` – vrajs5 Jun 18 '14 at 12:31
  • You should be able to do this using `panel.number()`. Reproducible examples of its use are given [here](http://stackoverflow.com/questions/9746597/changing-background-color-in-xyplot/9752056#9752056) and [here](http://stackoverflow.com/questions/12828027/conditional-panel-function-in-lattice-for-multiple-y-variables/16577667#16577667). – Josh O'Brien Jun 18 '14 at 13:57
  • I'm a huge lattice fan, but lattice really only likes to style things by group though you can create custom panel functions for each conditioning level as Josh suggested. This is one of the rare cases which I might suggest ggplot as an alternative. But i must admit i'm still a bit uncertain as to what your final plot should look like. – MrFlick Jun 18 '14 at 14:20
  • Alternatively, if you want to use the `pch` and `col` columns it sounds like you've added to your data.frame, use the `subscripts` variable, following the pattern [given in this answer](http://stackoverflow.com/questions/12828027/conditional-panel-function-in-lattice-for-multiple-y-variables/16577667#16577667). – Josh O'Brien Jun 18 '14 at 14:39

1 Answers1

3

This is best accomplished using panel.number(), which gives you access, inside the call to a custom panel function, to the index of the panel being currently plotted.

Here's a simple reproducible example:

library(lattice)
lattice.options(default.theme = standard.theme(color = FALSE))
COLORS = c("blue", "gold", "red", "green", "black", "grey")

xyplot(mpg ~ disp | carb, data = mtcars,
       panel = function(x,y,...) { 
           panel.xyplot(x, y, col=COLORS[panel.number()], pch=16, ...)
       })

enter image description here

A couple more involved examples are given here and here.

Community
  • 1
  • 1
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • This example doesn't have a `groups=` parameter. Though it's unclear to me from the OP's description exactly what he's doing with the different groups if he's not coloring points with them but he does have `auto.key` on to put them in the legend. – MrFlick Jun 18 '14 at 14:19
  • @MrFlick To add a grouping factor, just add `groups=cyl`, and remove `pch=16` from the custom panel function. – Josh O'Brien Jun 18 '14 at 14:28