Tips For Creating Barplots in R

If you want nice, professional-looking, publication quality barplots, you can't do much better than to prepare them in R. I don't have the time, the energy, or the knowledge to take you through everything that can be done to a barplot in R, but I can show you how to make one that will be suitable for any student paper you may be preparing.

The ucb data frame describes admissions into the six most popular grad programs at UC Berkeley in 1973. The dataset is built into R as a 3-way table (see UCBAdmissions, or type that at the prompt), but I have converted it into a data frame for use in our class.

> str(ucb)
'data.frame':   4526 obs. of  3 variables:
 $ admit: Factor w/ 2 levels "Admitted","Rejected": 1 1 1 1 1 1 1 1 1 1 ...
 $ sex  : Factor w/ 2 levels "Female","Male": 2 2 2 2 2 2 2 2 2 2 ...
 $ dept : Factor w/ 6 levels "A","B","C","D",..: 1 1 1 1 1 1 1 1 1 1 ...

The data frame contains 4526 observations on 3 variables: admit, sex, dept. A very basic barplot of the number of students applying to each department can be done easily.

> with(ucb,table(dept))->dept.table         # a barplot MUST be done on a table or matrix
> barplot(dept.table)

Now we can dress this up with some options.

  • main="title here" adds a main title at the top of the graph
  • xlab="label for x-axis here" adds a label on the x-axis
  • ylab="label for y-axis here" adds a label on the y-axis

You can type the titles and labels within the barplot command as such...

> barplot(dept.table,main="Applications by Department")

However, this can get ungainly if you have a lot of options to add. So the title for the graph can also be created as a separate object like this...

> title="Applications by Department"
> barplot(dept.table,main=title)

In this case, since title is a defined variable name with an assigned value, do NOT put the word title in quotes. Go ahead and try it to see what happens. Labels for the axes can be defined similarly...

> xlab="Department"
> ylab="No. of Applications"
> title="Applications by Department"
> barplot(dept.table,main=title,xlab=xlab,ylab=ylab)

If you don't like the y-axis stopping before the top of the highest bars, you can alter that as well. Make the y-axis as long or short as you want with the ylim option...

> barplot(dept.table,main=title,xlab=xlab,ylab=ylab,ylim=c(0,1200))       # ylim is a vector of starting and stopping coordinates

Do you want the bars to be colored? The option is col. My advice is not to get carried away with this, however. If you want to see all the color names R recognizes, type colors() at the prompt.

> col=c("red","orange","yellow","green","blue","violet")       # col is also a vector, btw
> barplot(dept.table,main=title,xlab=xlab,ylab=ylab,ylim=c(0,1200),col=col)

Do you want a horizontal axis with tick marks? Set the option axis.lty equal to 1. (That's the number one, not the letter "el".)

> barplot(dept.table,main=title,xlab=xlab,ylab=ylab,ylim=c(0,1200),col=col,axis.lty=1)

To see more options, type help(barplot) at the prompt. You can rename the categories on the x-axis. You can change the fonts and font sizes. You can... pretty much do whatever you want.

To plot a bar graph of two variables on the same graph, you need to begin with a contingency table or crosstabulation.

> with(ucb,table(sex,dept))->sex.dept.table
> barplot(sex.dept.table,main=title,xlab=xlab,ylab=ylab,ylim=c(0,1200),axis.lty=1)

This results in what is called a "stacked" bar graph. You'll want to add a legend to it so you can tell which shading applies to which gender. The option is legend=T.

> with(ucb,table(sex,dept))->sex.dept.table
> barplot(sex.dept.table,main=title,xlab=xlab,ylab=ylab,ylim=c(0,1200),axis.lty=1,legend=T)

To plot the gender bars side by side, set beside=T as well.

> barplot(sex.dept.table,main=title,xlab=xlab,ylab=ylab,ylim=c(0,1200),axis.lty=1,legend=T,beside=T)

And now for my next trick, I'm going to move the legend -- as soon as I figure out how!

Return to PSYC 480 Main Page