Bob Green
2008-Jun-13 11:14 UTC
[R] Switching the order of legend boxes in a lattice bar graph
I suspect there is a simple solution to this problem, but have been unable to find it. Below is some code that I have run to create 3 lattice graphs. I have been asked to change the legend so that the 'No' and dark blue are above "Y" and light blue in the legend to mirror the stacked bars in the graph which feature dark blue above light blue. I have tried changing the data as well as the order of the legend text, without success. Any assistance is much appreciated, regards Bob Green library(lattice) SNFP1 <- as.table(matrix(c(4,1, 4,4, 1,3, 2,7, 1,6, 0,4), ncol = 6, dimnames = list(group=c("Y","No"), Status=c("A","B", "C", "D", "E", "F")))) barplot(SNFP1, beside=FALSE, legend=TRUE, ylim=c(0, 60), ylab="N of patients", main ="district 1", col=c("light blue", "dark blue")) # "A","B", "C", "D", "E", "F" SNFP2 <- as.table(matrix(c(3,7, 1,5, 0,1, 0,1), ncol = 4, dimnames = list(group=c("Y","No"), Status=c("G","H", "I", "J")))) barplot(SNFP2, beside=FALSE, legend=TRUE, ylim=c(0, 60), ylab="N of patients", main ="district 2", col=c("light blue", "dark blue")) # "G", "H", "I", "J", SNFP3 <- as.table(matrix(c(3,0, 0,2, 3,4), ncol = 3, dimnames = list(group=c("Y","No"), Status=c("K","L", "M")))) barplot(SNFP3, beside=FALSE, legend=TRUE, ylim=c(0, 60), ylab="N of patients", main ="district 3", col=c("light blue", "dark blue")) df1 <- as.data.frame(t(SNFP1)) df2 <- as.data.frame(t(SNFP2)) df3 <- as.data.frame(t(SNFP3)) stuff <- make.groups(A=df1, B=df2, C=df3) # simple version barchart(Freq ~ Status | which, groups=group, data=stuff, stack=TRUE,scales=list(x=list(relation="free")), auto.key=TRUE) # advanced version barchart(Freq ~ Status | which, groups=group, data=stuff, stack=TRUE, as.table=TRUE, layout=c(2,2), skip=c(F,T,F,F),scales=list(x=list(relation="free")), ylab="patients", main="Figure 1: X by district", par.settings=list(superpose.polygon=list(col=c("light blue", "dark blue"))), auto.key=list(x = .6, y = .7, corner = c(0, 0)))
Markus Gesmann
2008-Jun-13 11:54 UTC
[R] Switching the order of legend boxes in a lattice bar graph
Hi Bob, Would this: mykey <- list( rectangles = list(col=c("dark blue","light blue") ), text=list(lab=c("No","Yes")),x = .6, y = .7, corner = c(0, 0)) barchart(Freq ~ Status | which, groups=group, data=stuff, stack=TRUE, as.table=TRUE, layout=c(2,2), skip=c(F,T,F,F),scales=list(x=list(relation="free")), ylab="patients", main="Figure 1: X by district", par.settings=list(superpose.polygon=list(col=c("light blue", "dark blue"))), key=mykey) solve your problem? Regards, Markus Markus Gesmann ?Associate Director?Libero Ventures Ltd, One Broadgate, London EC2M 2QS tel: +44 (0)207 826 9080? dir: +44 (0)207 826 9085?fax: +44 (0)207 826 9090 ?www.libero.uk.com A Lehman Brothers Company AUTHORISED AND REGULATED BY THE FINANCIAL SERVICES AUTHORITY -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Bob Green Sent: 13 June 2008 12:14 To: r-help at r-project.org Subject: [R] Switching the order of legend boxes in a lattice bar graph I suspect there is a simple solution to this problem, but have been unable to find it. Below is some code that I have run to create 3 lattice graphs. I have been asked to change the legend so that the 'No' and dark blue are above "Y" and light blue in the legend to mirror the stacked bars in the graph which feature dark blue above light blue. I have tried changing the data as well as the order of the legend text, without success. Any assistance is much appreciated, regards Bob Green library(lattice) SNFP1 <- as.table(matrix(c(4,1, 4,4, 1,3, 2,7, 1,6, 0,4), ncol = 6, dimnames = list(group=c("Y","No"), Status=c("A","B", "C", "D", "E", "F")))) barplot(SNFP1, beside=FALSE, legend=TRUE, ylim=c(0, 60), ylab="N of patients", main ="district 1", col=c("light blue", "dark blue")) # "A","B", "C", "D", "E", "F" SNFP2 <- as.table(matrix(c(3,7, 1,5, 0,1, 0,1), ncol = 4, dimnames list(group=c("Y","No"), Status=c("G","H", "I", "J")))) barplot(SNFP2, beside=FALSE, legend=TRUE, ylim=c(0, 60), ylab="N of patients", main ="district 2", col=c("light blue", "dark blue")) # "G", "H", "I", "J", SNFP3 <- as.table(matrix(c(3,0, 0,2, 3,4), ncol = 3, dimnames list(group=c("Y","No"), Status=c("K","L", "M")))) barplot(SNFP3, beside=FALSE, legend=TRUE, ylim=c(0, 60), ylab="N of patients", main ="district 3", col=c("light blue", "dark blue")) df1 <- as.data.frame(t(SNFP1)) df2 <- as.data.frame(t(SNFP2)) df3 <- as.data.frame(t(SNFP3)) stuff <- make.groups(A=df1, B=df2, C=df3) # simple version barchart(Freq ~ Status | which, groups=group, data=stuff, stack=TRUE,scales=list(x=list(relation="free")), auto.key=TRUE) # advanced version barchart(Freq ~ Status | which, groups=group, data=stuff, stack=TRUE, as.table=TRUE, layout=c(2,2), skip=c(F,T,F,F),scales=list(x=list(relation="free")), ylab="patients", main="Figure 1: X by district", par.settings=list(superpose.polygon=list(col=c("light blue", "dark blue"))), auto.key=list(x = .6, y = .7, corner = c(0, 0))) ______________________________________________ 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. This message is intended for the personal and confidential use for the designated recipient(s) named above. If you are not the intended recipient of this message you are hereby notified that any review, dissemination, distribution or copying of this message is strictly prohibited. This communication is for information purposes only and should not be regarded as an offer to sell or as a solicitation of an offer to buy any financial product, an official confirmation of any transaction or as an official statement of Libero Ventures Ltd. Email transmissions cannot be guaranteed to be secure or error-free. Therefore we do not represent that this information is complete or accurate and it should not be relied upon as such. All information is subject to change without notice.