Skip to Main Content

BSCI 1511L Statistics Manual: 0.2.2 Creating a bar chart with error bars using RStudio

Introduction to Biological Sciences lab, second semester

The Environment window

The upper right window of RStudio keeps track of what's been going in your work history.  The Environment tab keeps a list of the datasets that you have worked with previously.  If you click on the name of a dataset, it will be loaded into the Editor window in the upper left side of the screen.  

The image above shows what happens when the "Data" dataset name was clicked on.  It is also possible to import new datasets that haven't previously been used via the "Import Dataset" button or Tools menu item.  

Assuming that you completed the t-test of means example in the previous section, you should be able to verify that the "Data" dataset exists in RStudio's environment.  

Installing packages

One of the things that makes R so powerful is that you can make use of work that has already been done by others.  There are many powerful sets of functions, called "packages" that you can easily download to make your life easier.  Many of them are included in the Comprehensive R Archive Network (CRAN) that you may have used previously to download R.

We are going to use a widely used and extremely useful package that allows you to easily create and customize plots of various sorts.  It's called "ggplot2".  We will also use a second package that is required for some features of ggplot2.  It's called "Hmisc".  It's possible that the computer you are using already has ggplot2 and Hmisc loaded.  To determine this, go to the bottom right section of the RStudio window and select the “Packages.” tab.  

In the search bar type ggplot2. If you find ggplot2, click the check box next to it and repeat for Hmisc.  You can then skip to the next box.  

If you don't find the ggplo2 or Hmisc packages, you will need to download them.  Click on the "Packages" tab in the lower right window, then click Install.  If prompted to create a personal library, click Yes.  

An Install Packages window will pop up.  You can leave the Install from: option at its default "Repository (CRAN..".  In the Packages box, type

ggplot2

A bunch of lines will scroll up the console window.  When it says "The downloaded binary packages are in…" you're done.  You should also install the package

Hmisc

Once both the ggplot2 and Hmisc packages are installed, select them as described above.

Making a bar chart with error bars

Once you have verified that the dataset you want to use is present in the environment and that ggplot2 and Hmisc are enabled, it is a simple matter to create the bar chart.  In the lower left Console window, enter the following command:

ggplot(data=Data, aes(x=grouping, y=height, fill=grouping))+ stat_summary(fun.y = mean, geom = "bar") + stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width = 0.3)

If the command is successful, a bar chart with 95% confidence intervals will appear in the lower right window under the Plots tab.

Here's what you need to know to hack this command:

ggplot(data=[name of dataset], aes(x=[name of column to use for grouping bars], y=[name of column to use for y-axis value], fill=[name of column to use for determining bar color]))+ stat_summary(fun.y = mean, geom = "bar") + stat_summary(fun.data = [value to be used for error bars], geom = "errorbar", width = 0.3)

To use a different dataset, replace the red values above with ones appropriate for your data table.  The example shows uses "mean_cl_normal" to produce error bars for 95% confidence intervals.  Use "mean_sdl" for standard deviation error bars or "mean_se" for standard error of the mean error bars.

There are other options that can be used to change the width of the bars, whether the bars are outlined in black, etc. You can experiment with it and Google to find out more about how to embellish the plot.  

You can also set the values of the axis labels to be something other than the defaults taken from the column headers by adding a labs() function to the command.  

ggplot(data=Data, aes(x=grouping, y=height, fill=grouping))+ stat_summary(fun.y = mean, geom = "bar") + stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width = 0.3) + labs(x = "gender", y = "height (cm)")

 

How to save your chart

If you want a quick and dirty way to get your plot into a Word document or some other place where copy and paste is easy, you can use Windows Snipping Tool or some other kind of screen capture software to grab the image from the screen.

You can also use the Export function to copy or save the plot.  If you chose the Save Plot as Image option, you can save the plot in a variety of image formats and also mess with the plot resolution.

 

Thanks to Jacob Steenwyk for his help with this.