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))
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@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@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]]
Hello,
Maybe the following will help.
layout(matrix(c(1,2,3,0,4,5,6), c(1,6)))
Note the zero. It reserves space but no graph is put there.
There is an error in your code, you plot twice guns/gender. I think the
second is meant to be guns/age.
Also, you can do those 6 barplot instructions like this:
lapply(c("gender", "age"), function(y) {
lapply(c("guns", "crime", "climate"), function(x)
barplot(prop.table(table(dat[[x]], dat[[y]]), 2)))})
Hope this helps,
Rui Barradas
Em 31-01-2013 15:42, Simon Kiss escreveu:> 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.
>