[R] Help with multiple barplots

Duncan Mackay mackay at northnet.com.au
Thu Jan 31 21:56:31 CET 2013


Hi

Another possibility following on from Rich's example and to 
demonstrate what lattice can do with  the latticeExtra package

library(lattice)
library(latticeExtra)
useOuterStrips(
barchart(Freq ~ Var1 | topic + category, groups=Var2, data=opinion,
          stack=TRUE,
          layout=c(3,2),
          scales=list(x=list(relation="free")))
)

Regards

Duncan

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au



At 02:29 1/02/2013, you wrote:
>Simon,
>
>I think this is what you are looking for.
>
>###Random Data
>crime <- sample(c('agree' ,'disagree'), replace=TRUE, size=100)
>guns <- sample(c('agree','disagree'), replace=TRUE, size=100)
>climate <- sample(c('agree', 'disagree'), replace=TRUE, size=100)
>gender <- sample(c('male','both' ,'female'), replace=TRUE, size=100)
>age <- sample(c('old', 'neither', 'young'), replace=TRUE, size=100)
>
>dat <- as.data.frame(cbind(crime, guns, climate, gender, age))
>rm(crime, guns, climate, gender, age)
>
>opinion <-
>   rbind(
>     data.frame(table(dat[,4], dat[,1]), topic="crime", category="gender"),
>     data.frame(table(dat[,4], dat[,2]), topic="guns", category="gender"),
>     data.frame(table(dat[,4], dat[,3]), topic="climate", category="gender"),
>     data.frame(table(dat[,5], dat[,1]), topic="crime", category="age"),
>     data.frame(table(dat[,5], dat[,2]), topic="guns", category="age"),
>     data.frame(table(dat[,5], dat[,3]), topic="climate", category="age")
>     )
>
>
>library(lattice)
>barchart(Freq ~ Var1 | topic + category, groups=Var2, data=opinion,
>stack=TRUE,
>          layout=c(3,2), scales=list(x=list(relation="free")))
>
>
>Rich
>
>On Thu, Jan 31, 2013 at 10:42 AM, Simon Kiss <sjkiss at gmail.com> wrote:
>
> > Hello: I need to create a six barplots from data that looks pretty close
> > to what appears below. There are two grouping variables (age and gender)
> > and three dependent variables for each grouping variables.  I'm not really
> > familiar with trellis graphics, perhaps there is something that can do what
> > I need there, i don't know.
> > The thing is: I *need* these to appear on one row, with some way of
> > differentiating between the three barplots of one grouping variable and the
> > three from the other grouping variable.  It's for a grant application and
> > space is at a premium.  The width of everything can be about 7 inches wide
> > and the height maybe 2 to 2.5 inches. I also need an outer margin to place
> > a legend.  I can do this with the following using the layout command, but I
> > cannot figure out a nice way to differentiate the two groups of variables.
> >  I'd like to find a way to put a little bit of space between the three from
> > one grouping variable and the three from another grouping variable.
> >
> > If anyone has any thoughts, I'd be very grateful. Yours truly, Simon J.
> > Kiss
> >
> > ###Random Data
> > crime<-sample(c('agree' ,'disagree'), replace=TRUE, size=100)
> > guns<-sample(c('agree','disagree'), replace=TRUE, size=100)
> > climate<-sample(c('agree', 'disagree'), replace=TRUE, size=100)
> > gender<-sample(c('male','both' ,'female'), replace=TRUE, size=100)
> > age<-sample(c('old', 'neither', 'young'), replace=TRUE, size=100)
> > dat<-as.data.frame(cbind(crime, guns, climate, gender, age))
> > ###Code I'm working with now
> > layout(matrix(c(1,2,3,4,5,6), c(1,6)))
> > barplot(prop.table(table(dat$guns, dat$gender), 2))
> > barplot(prop.table(table(dat$crime, dat$gender), 2))
> > barplot(prop.table(table(dat$climate, dat$gender), 2))
> > barplot(prop.table(table(dat$guns, dat$gender), 2))
> > barplot(prop.table(table(dat$crime, dat$age), 2))
> > barplot(prop.table(table(dat$climate, dat$age), 2))
> >
> > ______________________________________________
> > 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.
> >
>
>         [[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