[R] Adding a vertical line to plot with two overlapping density plots

Petr PIKAL petr.pikal at precheza.cz
Wed Jan 4 16:55:38 CET 2012


Hi

> 
> Hi,
> A simple question I hope. I wish to add a single vertical line to a plot
> with several density plots.
> Here is a simplified example.
> 
> ############
> thedata <- data.frame(x1=rnorm(100,1,1),x2=rnorm(100,3,1)) #create data
> thedata.m<-melt(thedata)
> densityplot(~value, thedata.m, groups=variable,auto.key=list(columns=2))
> #this gives the two density plots
> #########
> 
> what I wish now is to add a vertical line, for example v=2.

If you mean densityplot from lattice here is a function (addapted from 
Gabor Grothendieck's code] which adds arbitrary line(s) into panels. Is 
options are similar to abline and once=TRUE means you want different lines 
to different panels.

> addLine
function (a = NULL, b = NULL, v = NULL, h = NULL, ..., once = FALSE) 
{
    tcL <- trellis.currentLayout()
    k <- 0
    for (i in 1:nrow(tcL)) for (j in 1:ncol(tcL)) if (tcL[i, 
        j] > 0) {
        k <- k + 1
        trellis.focus("panel", j, i, highlight = FALSE)
        if (once) 
            panel.abline(a = a[k], b = b[k], v = v[k], h = h[k], 
                ...)
        else panel.abline(a = a, b = b, v = v, h = h, ...)
        trellis.unfocus()
    }
}

Regards
Petr



> 
> Many thanks,
> Josh.
> 
>    [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> 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.



More information about the R-help mailing list