[R] Question about barplot: gridlines & value labels

Duncan Mackay mackay at northnet.com.au
Sun May 17 04:44:49 CEST 2009


Dimitri

You mentioned lattice so I presumed that you tried a barchart in lattice

if you found the class of table.a.percents will it comply with the lattice 
requirements of the data type ?
  class(table.a.percents)
[1] "matrix"

Using the lattice package and converting to a data.frame
x <- data.frame(Group = rep(paste("Group", 1:4), 5), a = 
rep(1:5,each=4),percentage = as.vector(table.a.percents))
  barchart(percentage~a,x,groups = Group, horizontal = F, auto.key = T, 
ylim = c(0,60))

will get you started in lattice, and customize as required
see also ?make.groups

PS ?sweep may be of interest in your data manipulation

Regards

Duncan Mackay
Department of Agronomy and Soil Science
University of New


At 04:57 17/05/2009, you wrote:
>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
>
>______________________________________________
>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