All arguments must have the same length error when trying to create a barplot.

Hi, I'm trying to create a bar chart for level of education. Originally, I produced a bar chart using:

> barplot(table(d1$EDUC2), main = "Level of Education", xlab = "Educational Attainment", ylab = "Frequency")

which produced a fine chart, except not all of my labels were included on the x-axis (the labels are "Above a bachelor's degree", "Bachelor's degree", "High school diploma or less", "Postsecondary certificate or diploma", and "Some postsecondary" but the only labels that were on my bar chart were "Above a bachelor's degree" and "Some postsecondary").

So I looked at some resources for help on getting all the labels included. The following modifications were recommended, so I ended up with:

> barplot(table(d1$EDUC2, las = 2, cex.names=0.5), main = "Level of Education", xlab = "Educational Attainment", ylab = "Frequency", names.arg = c("Above a Bachelor's Degree", "Bachelor's Degree", "High School Diploma or Less", "Postsecondary Certificate or Diploma", "Some Postsecondary"))

which resulted in the following error:
Error in table(d1$EDUC2, las = 2, cex.names = 0.5) : all arguments must have the same length.

I also get the error using just las = 2, just cex.names = 0.5, names.arg = c(), and every combination of the functions.

Does anyone know what is going wrong? I just want to see all my labels on the x-axis for my graph :frowning:

Hi @kallenby,
Welcome to the RStudio Community Forum.

Its a bit tricky to get a "sensible" barplot because your x-axis category labels are very long. This code works but I've used dummy data in a dataframe because we don't have your table() data. The trick is to increase the bottom margin to make room for the labels. Also, the x-axis label will be over-written by this approach (I've made it blank). See help(mtext) if you still need an x-axis label.

# Make up some data in a dataframe
dat <- data.frame(edu_level = c("Above a bachelor's degree", 
                                "Bachelor's degree",
                                "High school diploma or less", 
                                "Postsecondary certificate or diploma",
                                "Some postsecondary"),
                  freq = c(23,15,67,45,33))
#>                              edu_level freq
#> 1            Above a bachelor's degree   23
#> 2                    Bachelor's degree   15
#> 3          High school diploma or less   67
#> 4 Postsecondary certificate or diploma   45
#> 5                   Some postsecondary   33

# increase margins to add room for the rotated labels
par(mar = c(12, 4, 2, 2) + 0.2)
barplot(freq ~ edu_level, data=dat, 
        main = "Level of Education",
        xlab = "", 
        ylab = "Frequency",
        las = 2,
        cex.names = 0.8)

Created on 2021-02-22 by the reprex package (v1.0.0)


Hi @DavoWW,

Thank you so much for your help, that worked out!! I figured it was an issue with the x axis label length, so that solves my problem!

Thanks again!