I am trying to add a two vertical lines to a barchart I made, but am having trouble.
Examples data set
data<-data.frame(area=rep(c("ES","OC","VB"),each=2,times=2),num=c(0,10,23,40,25,60,80,45,10,25,10,0),
bin=rep(c(85,90),times=3))
barchart code. Most of the formatting code is for the actual data set.
require(lattice)
barchart(num~as.factor(bin), data=data,col=c("black","lightslategray","light gray"),
groups = area, stack = F, horizontal=F,xlab="Shell Length (mm)",ylab="Frequency (N)",
scales=list(y=list(alternating=1,tck=-1,limits=c(0,400),
at=c(0,50,100,150,200,250,300,350,400),
labels=c("0","50","100","150","200","250","300","350","400"),relation="same")),
par.settings = list( grid.pars = list(fontfamily = 'serif'),axis.line=list(col=0)),
panel = function(...) {
lims<-current.panel.limits()
panel.barchart(...)
panel.abline(v=as.factor(80),col="red",lty=2)
panel.abline(v=as.factor(90),h=0,col="black")
panel.abline(h=lims$ylim[1],v=lims$xlim[1])
panel.text(25,200,"OC (N = 1,947)",col="black",cex=.8)
panel.text(25,185,"ES (N = 2,623)",col="lightslategray",cex=.8)
panel.text(25,170,"VB (N = 2,780)",col="light gray",cex=.8)
})
When I run the code one vertical line in on the graph, but the second line does not show up. Regardless of what value I put as v the one line does not change position.
R information
R version 3.2.1 (2015-06-18)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] grid stats graphics grDevices utils datasets
[7] methods base
other attached packages:
[1] plyr_1.8.3 Hmisc_3.17-0 ggplot2_1.0.1
[4] Formula_1.2-1 survival_2.38-1 car_2.0-26
[7] MASS_7.3-40 xlsx_0.5.7 xlsxjars_0.6.1
[10] rJava_0.9-7 latticeExtra_0.6-26 RColorBrewer_1.1-2
[13] lattice_0.20-31
Any help would be appreciated. Thanks
You convert bin
to a factor at the beginning of your code (although it would be coerced to a factor even if you didn't) creating a categorical axis with implicit primary tick coordinates of 1 and 2 as the center of each group of bars, rather than 85 and 90 as you were expecting. See for example what happens when you change your code as follows:
panel.abline(v=1,col="red",lty=2)
panel.abline(v=2,h=0,col="black")
panel.abline(h=lims$ylim[1],v=lims$xlim[1])
panel.text(1,200,"OC (N = 1,947)",col="black",cex=.8)
panel.text(1.5,185,"ES (N = 2,623)",col="lightslategray",cex=.8)
panel.text(2,170,"VB (N = 2,780)",col="light gray",cex=.8)
})