Creating bar charts

A bar chart is the simplest chart that displays the frequency of a categorical variable or the summary statistics of a numeric variable over the category of other variables. In this recipe, we will learn how we can produce a bar chart using the ggplot2 library.

Let's call the ggplotdata dataset that was created in the preceding section. We intend to produce a bar chart that will represent the mean of a numeric variable on the y axis over the category of the economic status variable on the x-axis. So, we need to prepare the summarized dataset as follows:

The description of the most important components of the preceding function is as follows:

Now, let's call the ggplot2 library using the following line of code:

The standard code that produces a bar chart is as follows:

How to do it…

Each of the code's arguments is described as follows:

We can change the width of the bar and also change the orientation of the bar using some of the arguments with the primary function. An example of this is shown in the following code:

The plot will look like what's shown in the following figure:

There's more…

To produce a bar chart for more than one numeric variable, follow the Creating multiple bar charts recipe; to produce a bar chart with error bars, refer to the Creating a bar chart with error bars recipe.