An Advanced Bar Graph Example Using R And ggplot2

Hi. I have done experimentation with R and ggplot2 and have come up with a bar graph which combines many of ggplot2’s bar graph features.


Table Of Contents

The Suicide Data

The Bar Plot

Considering Gender With Facet Grid Bar Graph

Fixing The Facet Grid Bar Graph


The Suicide Data

For this example, I have chosen a dataset with the topic of suicide. This particular dataset can be found in the faraway library in R. The lines of code below shows the loading of the ggplot2 and faraway libraries.

From the faraway package the suicide dataset is under the name suicide. I save the data suicide under the variable suicide_data. I then preview the data and check the data structure using head(), tail() and str() respectively.

The screenshot image below is from page 97 of this link provides a description and detailed information of the suicide dataset.

From head() and tail(), you can see that the column titles are not the greatest. You can rename column titles using colnames() in R.


The Bar Plot

I wanted my bar graph to look at the cause of death, the counts associated with the cause of death and I wanted to put all the three age groups in it too. This code allows for the creation of the bar graph visual below.

Here are some notes about the code and the graph above:

  1. I have aes(x = Cause, y = Count, fill = Age) for Cause of Death on the bottom, Count for the bar lengths and fill = Age for the different bar colours.
  2. In geom_bar(), I have stat = “identity”. Having position = “dodge” is crucial for the side by side bars.
  3. The labs() function allows for labels.
  4. The scale_x_discrete() function allows for labeling my x-values in a neat way. If this scale_x_discrete() function is not there, I think it shows the x-values from the table such as drug, gun, other, etc.
  5. scale_fill_discrete(labels=c(“Middle Aged”, “Old”, “Young”) allows for labeling the colours in the legend.
  6. The theme() function allows for custom appearances such as font colours, font text and title centering.


Considering Gender With Facet Grid Bar Graph

The above graph is very nice indeed but it does not consider gender. We can create an alternative bar graph which includes gender on top of age group and cause of death.

The code and output can be found below.

The facet_grid(. ~Sex) allows for separate bar graphs by sex. The strip.background and strip.text attributes in theme() allows for customization of the bars associated with f and m.

This facet_grid output really helps in comparing the two graphs. One can easily see that male suicide counts are much higher than female suicide rates. The cause of death which really stands out in death by hanging for males denoted by Hang on the right graph. The number of young males who have committed suicide is quite alarming.


Fixing The Facet Grid Bar Graph

The only problem with the bar graph above is the f and m. I would like Female and Male instead.

Converting f and m to Female and Male will require a labeller. I have used the reference link http://stackoverflow.com/questions/3472980/ggplot-how-to-change-facet-labels to come up with the code below. The code below uses a list (which is like a dictionary in Python, correct me if I am wrong here). and a function called gender_labeller.

This visual looks pretty good now as the labels are fixed.

Leave a Reply