1

I found coplot {graphics} very useful for my plots. However, I would like to include there not only one line, but add there one another. For basic graphic I just need to add = TRUE to add another line, or tu use plot(..) and lines(..). For {lattice} I can save my plots as objects

a<-xyplot(..)
b<-xyplot(..)

and display it simply by a + as.layer(b). No one of these approaches works for coplot(), apparently because creating objects as a<-coplot() doesn't produce trellis graphic but NULL object.

Please, any help how to add data line in coplot()? I really like its graphic so I wish to keep it. Thank you !!

my exemle data are here: http://ulozto.cz/xPfS1uRH/repr-exemple-csv

My code:

sub.tab<-read.csv("repr_exemple.csv", , header = T, sep = "")

attach(sub.tab) 

cells.f<-factor(cells, levels=c(2, 25, 100, 250, 500),      # unique(cells.in.cluster)???
    labels=c("size2", "size25", "size100", "size250", "size500"))

perc.f<-factor(perc, levels=c(5, 10),      # unique(cells.in.cluster)???
    labels=c("perc5", "perc10"))

# how to put these plots together?
a<- coplot(max_dist ~ time |cells.f  + perc.f, data = sub.tab, 
    xlab = "ticks", type = "l", col = "black", lwd = 1)   

b<- coplot(mean_dist ~ time |cells.f  * perc.f, data = sub.tab, 
    xlab = "ticks", type = "l", col = "grey", lwd = 1) 

a + as.layer(b)  # this doesn't work

Please, how to merge these two plots (grey and black lines)? I couldn't figure it out... Thank you !

enter image description here enter image description here

maycca
  • 3,848
  • 5
  • 36
  • 67
  • You probably should just specify a custom panel function. Please provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample data and code. Describe exactly what "line" you want to draw. – MrFlick Oct 13 '15 at 03:42
  • What is the `coplot` you use and which line do you want to add? –  Oct 13 '15 at 03:42
  • 1
    Are you sure `coplot` is a lattice function? It appears to be a [base graphics function](http://www.rdocumentation.org/packages/graphics/functions/coplot). – MrFlick Oct 13 '15 at 03:53
  • Hi @MrFlick, thank you, I have already corrected it. – maycca Oct 13 '15 at 04:10
  • I didn't even know `coplot()` existed. This is great! – thelatemail Oct 13 '15 at 05:52

1 Answers1

2

Linking to sample data isn't really as helpful. Here's a randomly created sample data set

set.seed(15)
dd <- do.call("rbind", 
    do.call("Map", c(list(function(a,b) {
        cbind.data.frame(a,b, x=1:5, 
        y1=cumsum(rpois(5,7)),
        y2=cumsum(rpois(5,9)))
    }), 
    expand.grid(a=letters[1:5], b=letters[20:22])))
 )
 head(dd)
#   a b x y1 y2
# 1 a t 1  8 16
# 2 a t 2 13 28
# 3 a t 3 25 35
# 4 a t 4 33 45
# 5 a t 5 39 57
# 6 b t 1  4 12

I will note the coplot is a base graphics function, not Lattice. But it does have a panel= parameter. And you can have the coplot() take care of subsetting your data for you (well, calculating the indexes at least). But, like other base graphics functions, plotting different groups isn't exactly trivial. You can do it in this case with

coplot(y~x|a+b, 
   # make a fake y col to cover range of all y1 and y2 values
   cbind(dd, y=seq(min(dd$y1, dd$y2), max(dd$y1, dd$y2), length.out=nrow(dd))), 
    #request subscripts to be sent to panel function
    subscripts=TRUE, 
    panel=function(x,y,subscripts, ...) {
        # draw group 1
        lines(x, dd$y1[subscripts])
        # draw group 2
        lines(x, dd$y2[subscripts], col="red")
})

This gives

enter image description here

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Thank you @MrFlick !! It takes me a moment to understand the concept of "panel" and "subscript" but it works great now !! :) – maycca Oct 13 '15 at 17:38
  • Is there a way to adapt this so as to pass the mean of the 'given' variable to the `panel` function? I'm trying to plot the fitted model (regression line) over the points in each panel, & I need to use a function of 'Given' (beta_1*mean(x1|Given)) to shift the intercept (see [How to plot regression or LOWESS lines over data in coplot](https://stackoverflow.com/q/53233176/1217536)). – gung - Reinstate Monica Nov 12 '18 at 22:21