Alexander.Herr at csiro.au
2007-Aug-09 04:51 UTC
[R] plot table with sapply - labeling problems
Hi List, I am trying to label a barplot group with variable names when using sapply unsucessfully. I can't seem to extract the names for the indiviual plots: test<-as.data.frame(cbind(round(runif(50,0,5)),round(runif(50,0,3)),roun d(runif(50,0,4)))) sapply(test, table)->vardist sapply(test, function(x) round(table(x)/sum(table(x))*100,1) )->vardist1 par(mfrow=c(1,3)) sapply(vardist1, function(x) barplot(x, ylim=c(0,100),main="Varset1",xlab=names(x))) par(mfrow=c(1,1)) Names don't show up although names(vardist) works. Also I would like to put a single Title on this plot instead of repeating "Varset" three times. Any hints appreciated. Thanx Herry
Here is a modified script that should work. In many cases where you want the names of the element of the list you are processing, you should work with the names: test<-as.data.frame(cbind(round(runif(50,0,5)),round(runif(50,0,3)),round(runif(50,0,4)))) sapply(test, table)->vardist sapply(test, function(x) round(table(x)/sum(table(x))*100,1) )->vardist1 par(mfrow=c(1,3)) # you need to use the 'names' and then index into the variable # your original 'x' did not have a names associated with it sapply(names(vardist1), function(x) barplot(vardist1[[x]], ylim=c(0,100),main="Varset1",xlab=x)) par(mfrow=c(1,1)) On 8/9/07, Alexander.Herr at csiro.au <Alexander.Herr at csiro.au> wrote:> Hi List, > > I am trying to label a barplot group with variable names when using > sapply unsucessfully. > I can't seem to extract the names for the indiviual plots: > > test<-as.data.frame(cbind(round(runif(50,0,5)),round(runif(50,0,3)),roun > d(runif(50,0,4)))) > sapply(test, table)->vardist > sapply(test, function(x) round(table(x)/sum(table(x))*100,1) )->vardist1 > par(mfrow=c(1,3)) > sapply(vardist1, function(x) barplot(x, > ylim=c(0,100),main="Varset1",xlab=names(x))) > par(mfrow=c(1,1)) > > Names don't show up although names(vardist) works. > > Also I would like to put a single Title on this plot instead of > repeating "Varset" three times. > > Any hints appreciated. > > Thanx > Herry > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?