[R] Adding a line to barchart

S Ellison S.Ellison at LGCGroup.com
Wed Jan 23 17:57:00 CET 2013


 

> > I need a quick help with the following graph (I'm a lattice newbie):
> > 
> > require("lattice")
> > npp=1:5
> > names(npp)=c("A","B","C","D","E")
> > barchart(npp,origin=0,box.width=1)
> > 
> > # What I want to do, is add a single vertical line 
> positioned at x = 2
> > that lays over the bars (say, using a dotted line).  How do 
> I go about
> > doing this?

In a lattice call, you can use panel= to provide a panel function that does more than one thing per panel. In this case:

npp=1:5
names(npp)=c("A","B","C","D","E")
barchart(npp,origin=0,box.width=1,
	panel=function(x, y, ...) {
	     panel.barchart(x, y, ...)
	     panel.abline(v=2, lty=2)
	}
)

This gets more useful with grouped data because the panel function picks up the grouping factor:

g <- gl(5,5)
h <- factor(rep(LETTERS[1:5], 5))
barchart(h~ppn|g,origin=0,box.width=1,
	panel=function(x, y, ...) {
	     panel.barchart(x, y, ...)
	     panel.abline(v=2, lty=2.5)
	}
)

*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}



More information about the R-help mailing list