Creating a bar chart with error bars

A bar chart with error bars is another commonly used data visualization technique. In this recipe, we will produce a bar chart with error bars.

To produce a bar chart with error bars, we need to process the dataset in such a way that we have the relevant data at hand. In fact, to produce the error bar, we need to have the standard error of the mean that can be transformed into the lower and upper limit. To make this happen, we will call ggplotdata first, and then will do some processing using the following code:

Now, we will use bardata to produce the bar chart with the error bar representing a 95 percent confidence interval of the mean.

To produce a bar chart with an error bar, firstly, we will create the basic bar chart with the following code and store it in an R object, which is errbar:

The preceding code will produce the bar chart, but it will not display the output until we call the object separately. Now, we will add the error bar with the basic chart as follows:

The plot looks like what is shown in the following figure:

How to do it…

The main mechanism of this plot is the data processing itself. We need to create the variables for each part of the bar chart. In this case, our objective was to create a bar chart of the mean of the disA variable over the category of economic status. At the same time, we wanted to create the error bar in order to represent a 95 percent confidence interval of each mean. So, we have created variables for the mean and the lower and upper limit of a 95 percent confidence interval.

Once we have the data in hand, we can just produce the bar chart following the basic command. To add the error bar, we need to add a layer to the existing plot using geom_errorbar(). Within geom_errorbar(), we used the lower and upper limit of the 95 percent confidence interval as the minimum and maximum value of the y-axis.

Initially, the bar width looks very thick, and we can easily control this using the width argument within the geom_bar() function. We can do the following with the basic bar chart:

Now, it's your turn to try the various options in order to play with the bar charts.