Frequencies
Bar plots are ideal for plotting frequencies. Although it is easy to create a simple bar plot, there are several things that can be done to improve on them:
- Order the categories in terms of their frequency
- Add frequency labels to make it easier to see the exact frequencies
There are two basic ways to construct a bar plot in ggplot
. The first way is to use the raw data and have ggplot
calculate the frequencies.
If you want to flip the order from high to low, remove the fct_rev()
function from the code.
The second way is to first calculate the frequencies yourself and then use the resulting data frame to plot the frequencies.
Percentages
If you want to plot percentages instead, you can use the same two techniques as before: 1) have ggplot
calculate the percentages or 2) do it yourself first.
If you want to have ggplot
calculate a percentage, you can use ..count../sum(..count..)
.
Alternatively, you calculate the percentages yourself first.
The code of this latter approach seems to be a lot simpler, so could be recommended on that basis.
Stacked bar plots
Stacked bar plots can be used to add additional information to a bar plot, but they generally are difficult to interpret. It’s difficult to assess the varying sizes of the stacked bar charts. Plotting Likert responses as a stacked bar char may be an exception, as long as it is accompanied by labeled percentages and rotated horizontally. See the Likert chapter for more information.
Usually the better way to add additional information to a bar plot is to use faceting.