Search code examples
rgraphplotlattice

Add a line to coplot {graphics}, classic approaches don't work


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


Solution

  • 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