[R] Question about barplot: gridlines & value labels

Dimitri Liakhovitski ld7631 at gmail.com
Sat May 16 20:57:07 CEST 2009


Hello!
I promise I looked into help files before asking. Still cannot figure
it out. I think it's because I am totally confused what packages use
lettice, which use trellis, etc.
Sections 1 and 2 below produce the data and the data to plot. My
question is about barplot in Section 3. I am trying to:
1. add only horizontal gridlines and manipulate the type and color of
that line. tck = 1 is not flexible enough to do it.
2. make the legend come on top of gridlines and not under them
3. add plotted Y values above the bars

Thank you very much for any pointers!
Dimitri

### Section 1: generates my data set "data":

N<-100
myset1<-c(1,2,3,4,5)
probs1<-c(.05,.10,.15,.40,.30)

group<-unlist(lapply(1:4,function(x){
	out<-rep(x,25)
	return(out)
}))
set.seed(1)
a<-sample(myset1, N, replace = TRUE,probs1)
a[which(rbinom(100,2,.01)==1)]<-NA
set.seed(12)
b<-sample(myset1, N, replace = TRUE,probs1)
b[which(rbinom(100,2,.01)==1)]<-NA
set.seed(123)
data<-data.frame(group,a=a,b=b)
data["group"]<-lapply(data["group"],function(x) {
	x[x %in% 1]<-"Group 1"
	x[x %in% 2]<-"Group 2"
	x[x %in% 3]<-"Group 3"
	x[x %in% 4]<-"Group 4"
	return(x)
})
data$group<-as.factor(data$group)
lapply(data,table,exclude=NULL)

### Section 2. Creating data to plot:
table.a<-with(data,table(group,a))
table.a.percents<-apply(table.a,2,function(x){
	out<-round(x*100/sum(x),1)
	return(out)
})

### Section 3. Creating a plot:
barplot(table.a.percents,xlab = "Values", ylab =
"Percentages",ylim=c(0,100),axis.lty=1,legend=T,beside=T,tck = 1)

-- 
Dimitri Liakhovitski
MarketTools, Inc.
Dimitri.Liakhovitski at markettools.com




More information about the R-help mailing list