Error In Barplot Incorrect Number Of Names
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up side by side bar plot group names up vote 0 down vote favorite 1 I have a table like this: counts[1:4,] aaa 0.0010 -0.0252 -0.0039 aac 0.0452 0.0168 -0.0238 aag -0.0117 -0.0029 0.0400 aat 0.0301 0.0248 0.0070 I want to make a bar plot, where the three columns for each row are next to each other, and the name for that three numbers are labeled by the row name. I have no problem when plotting three rows: barplot(counts[1:3,], col=c("red","lightblue","gray"), beside = T, names=row.names(counts)[1:3]) But when I plot the four rows (or more), R complains about the number of names is incorrect: "Error in barplot.default(counts[1:4, ], col = c("red", "lightblue", "gray"), : incorrect number of names" barplot(counts[1:4,], col=c("red","lightblue","gray"), beside = T, las=3, names.arg=row.names(counts)[1:4]) How can i plot these groups with one name for each group? r plot bar-chart names share|improve this question edited Jan 16 '14 at 10:38 Matt 10.1k43655 asked Jan 16 '14 at 10:35 user2783615 1091412 add a comment| 1 Answer 1 active oldest votes up vote 1 down vote accepted You got this error because with besides=T each column is plotted together (not the rows as you expect). To have beside all values of one row transpose your data with t() inside the barplot(). barplot(t(counts[1:4,]), col=c("red","lightblue","gray"), beside = T, las=3, names.arg=row.names(counts)[1:4]) share|improve this answer answered Jan 16 '14 at 10:55 Didzis Elferts 46.1k7100107 Thanks Didzis Elferts! –user2783615 Jan 16 '14 at 11:13 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email discard By posting your answer, you agree to the privacy policy and terms of service. Not the answer you're looking for? Browse other questions tagged r plot bar-chart names or ask your
files Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] On 01/15/2010 08:43 AM, kellys17 wrote: > > Hi, > > I have a data set that has columns "bird.species", "tree.species"and > "count". I am investigating differences in tree species usge by two species > of birds. "Count" is numerical and is a count of observations of bird > species x using tree species x. For bird species A I have 3 different counts > for each tree species. For bird species B I have only 1 count http://stackoverflow.com/questions/21159302/side-by-side-bar-plot-group-names for each tree > species. > > I have generated a barplot using the following function: > barplot(tapply(count,list(bird.species,tree.species),mean),beside=TRUE) > > This graph is so far ok and gives the mean count for bird species A. > I want to add error bars to illustrate the variance in mean of bird species > A for each tree species. > > The error bar function I am using https://stat.ethz.ch/pipermail/r-help/2010-January/224502.html is: > error.bars<-function(yv,z,nn){ > xv<-barplot(yv,ylim=c(0,(max(yv)+max(z))),names=nn, > ylab=deparse(substitute(yv))) > g<-(max(xv)-min(xv))/50 > for (i in 1:length(xv)){lines(c(xv[i],xv[i]),c(yv[i]+z[i],yv[i]-z[i])) > lines(c(xv[i]-g,xv[i]+g),c(yv[i]+z[i],yv[i]+z[i])) > lines(c(xv[i]-g,xv[i]+g),c(yv[i]-z[i],yv[i]-z[i]))}} > > I used "error.bars(ybar1,se1,labels1)" to try generate the error bars with > se1<-rep("standard error of a mean", "no. of numbers used to calculate > mean") > labels1<-as.character(levels(tree.species)) > ybar1<-as.vector(tapply(count,tree.species,mean)) > > but the it failed and the resulting warning I got was: > Error in barplot.default(yv, ylim = c(0, (max(yv) + max(z))), names = nn, : > incorrect number of names > > I was wondering If anyone could help with me this warning, or point me in > the right direction as to how to illustrate the variance in means from bird > species A without adding any error bars to bird species B (which has no > variance to show)? Hi Sean, The easiest way I can think of is to add a test to your error bar drawing routine so that it does nothing when the variance is NA: for(i in 1:length(xv)) { if(!is.na(z[i])) { lines... } } However, the error is about the number of names not being equal to the number of "heights", so you will have to make sure that
this? BioStatMatt11-08-2006, 11:59 AMIt will be difficult to determine what you are doing wrong without seeing the command you used and the output. Do you get error in errors? naivesincerity11-08-2006, 12:11 PM>barplot(Election8387929701,space=c(0,0.5),beside=TRUE,col=c("red","blue","yellow"),legend.text=c("Labour","Conservative","Liberal Democrats"),ylim=c(0,50),names.arg=year,ylab="Percentage of vote") naivesincerity11-08-2006, 12:13 PMBefore that was: > Election01=c(41.41,35.23,19.42) > Election83=c(26.94,45.98,14.36) > Election87=c(29.51,46.24,13.58) > Election92=c(33.93,45.46,19.18) > Election97=c(43.55,33.69,17.95) >year=c(1982,1987,1992,1997,2001) >Election8387929701=rbind(Election83,Election87,Election92,Election97,Election01) naivesincerity11-08-2006, 12:18 PMBut even when it does respond with error in barplot a '+' sign, as though the plot has been drawn, with no error message, I can't find the plot on the terminal naivesincerity11-08-2006, 12:42 PMError in barplot.default(Election9701, space = c(0, 0), beside = TRUE, : incorrect number of names In addition: Warning message: parameter "legend.txt" couldn't be set in high-level plot() function Got this message BioStatMatt11-08-2006, 05:42 PMtry changing the "rbind" to "cbind" like this: Election01=c(41.41,35.23,19.42) Election83=c(26.94,45.98,14.36) Election87=c(29.51,46.24,13.58) Election92=c(33.93,45.46,19.18) Election97=c(43.55,33.69,17.95) year=c(1982,1987,1992,1997,2001) Election8387929701=cbind(Election83,Election87,Election92,Election97,Election01) barplot(Election8387929701,space=c(0,0.5),beside= TRUE,col=c("red","blue","yellow"),legend.text=c("Labour","Conservative","Liberal Democrats"),ylim=c(0,50),names.arg=year,ylab="Perc entage of vote") If you indeed wanted groups of bars according to political party, it looks like you have used names.args and legend.tex
Sign in Pricing Blog Support Search GitHub This repository Watch 13 Star 7 Fork 2 allr/timeR Code Issues 0 Pull requests 0 Projects 0 Pulse Graphs Permalink Branch: master Switch branches/tags Branches Tags master vendor Nothing to show R-301 Nothing to show Find file Copy path timeR/src/library/graphics/R/barplot.R Fetching contributors… Cannot retrieve contributors at this time Raw Blame History 202 lines (183 sloc) 6.81 KB # File src/library/graphics/R/barplot.R # Part of the R package, https://www.R-project.org # # Copyright (C) 1995-2012 The R Core Team # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # A copy of the GNU General Public License is available at # https://www.R-project.org/Licenses/ barplot <- function(height, ...) UseMethod("barplot") barplot.default <- function(height, width = 1, space = NULL, names.arg = NULL, legend.text = NULL, beside = FALSE, horiz = FALSE, density = NULL, angle = 45, col = NULL, border = par("fg"), main = NULL, sub = NULL, xlab = NULL, ylab = NULL, xlim = NULL, ylim = NULL, xpd = TRUE, log = "", axes = TRUE, axisnames = TRUE, cex.axis = par("cex.axis"), cex.names = par("cex.axis"), inside = TRUE, plot = TRUE, axis.lty = 0, offset = 0, add = FALSE, args.legend = NULL, ...) { if (!missing(inside)) .NotYetUsed("inside", error = FALSE)# -> help(.) if (is.null(space)) space <- if (is.matrix(height) && beside) c(0, 1) else 0.2 space <- space * mean(width) if (plot && axisnames && is.null(names.arg)) names.arg <- if(is.matrix(height)) colnames(height) else names(height) if (is.vector(height) || (is.array(height) && (length(dim(height)) == 1))) { ## Treat vectors and 1-d arrays the same. height <- cbind(height) beside <- TRUE ## The above may look strange, but in particular makes color ## specs work as most likely expected by the users. if(is.null(col)) col <- "grey" } else if