[R] Using abline in lattice

weiflo fweiler08 at jhubc.it
Wed Oct 26 17:26:06 CEST 2011


Dear all,

being a relative beginner in R, I apologize for posting the second question
within two days.

So I want a stacked barchart, which should look like the one produced by
this code:
Tuvalu <- c(9,3,4,0,3,0,0)
Singapor <- c(38,0,0,0,12,19,0)
Samoa <- c(26,16,2,0,5,2,0)
PNG <- c(56,4,0,5,2,0,56)
Micronesia <- c(6,0,0,0,0,0,0)
graph4 <- data.frame(rbind(Tuvalu,Singapor,Samoa,PNG,Micronesia))
graph4$country <- c("Tuvalu","Singapore","Samoa","Papua New
Guinea","Micronesia")
graph4$country <- factor(graph4$country)
xyplot(country ~ X1 + X2 + X3 + X4 + X5 + X6 + X7, data=graph4,
         xlim=c(0,130),
         #scales = list(alternating = 1, cex=1.2),
         xlab="",
         panel = function (x,y) {
           stack=F
           groups=country
           panel.barchart(x,y,
col=c("grey20","grey100","grey50","grey83","grey33","grey67","grey0"))
           panel.abline(v = 20, lty = 2, col = "blue")
         }
       )


But now I would like to add vertical lines at certain values (20, 40, etc.),
but because I couldn't make the abline command work with the above code, I
wrote a panel function. Then the vertical lines work quite well, but now the
bars are plotted on top of each other. See for yourself, here is the code
(the first four lines of recoding I did to make sure that its not a problem
of the formula with the pluses, but it turns out just the same):

test <- data.frame(rep(graph4$country,7),
     
c(graph4$X1,graph4$X2,graph4$X3,graph4$X4,graph4$X5,graph4$X6,graph4$X7))
names(test) <- c("country","X1")
names(test)


xyplot(country ~ X1, data=test,
         xlim=c(0,130),
         #scales = list(alternating = 1, cex=1.2),
         xlab="",
         panel = function (x,y) {
           #groups=country
           panel.abline(v = 20, lty = 2, col = "grey70")
           panel.abline(v = 40, lty = 2, col = "grey70")
           panel.barchart(x,y,
col=c("grey20","grey100","grey50","grey83","grey33","grey67","grey0"))
           },
       )

I played a lot around with the stack command in the second code, nothing
worked. My question now would be, how can I either make the vertical lines
work with the first code, or the bars look like in the first example using
the second code.

Thanks a lot for your help!
Florian


--
View this message in context: http://r.789695.n4.nabble.com/Using-abline-in-lattice-tp3941012p3941012.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list