[R] Question about barplot: gridlines & value labels

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


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 ?
[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


Duncan Mackay
Department of Agronomy and Soil Science
University of New

At 04:57 17/05/2009, you wrote:
>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!
>### Section 1: generates my data set "data":
>         out<-rep(x,25)
>         return(out)
>a<-sample(myset1, N, replace = TRUE,probs1)
>b<-sample(myset1, N, replace = TRUE,probs1)
>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)
>### Section 2. Creating data to plot:
>         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
>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