Making Bar Graphs In R Using ggplot2

Hello. In this post, the ggplot2 package in R will be used to produce informative and nice bar graphs.


Table of Contents

  1. The Data: Simulating Dice Rolls
  2. Data Cleaning for Graphing
  3. Producing the Bar Graph
  4. Notes and Thoughts


The Data: Simulating Dice Rolls

No dataset will be imported here. We will generate some simulation results this time around. We will simulate rolling a fair six sided die 1000 times using the sample() function in R.

The table(result) function in R gives us the frequencies of the outcomes of this experiment.

The mode (or most frequent result) is rolling a three at 197 times. If another 1000 die rolls were made then it is not guaranteed we will replicate the same results. (The probability of replicating such results is very low.)


Data Cleaning for Graphing

Now that we have our results, we need to fix / clean our data such that it is nicely formatted for graphing purposes.

In the x-axis (horizontal) of our graph we do not really want to see 1,2,3,4,5,6. It would be a bit better to use “One”, “Two” up to “Six” for each bar. We create a vector in R with the names.

With the outcome names, we want the corresponding counts from the experiment.

Next, we combine the die_names vector and the outcome_data vector into a data frame. This data frame will be ready for graphing purposes.


Producing the Bar Graph

We now plot our bar graph using the ggplot() function from ggplot2. The labels of One, Two up to Six are on the x-axis and the counts / frequencies are on the y-axis. A title is added and we set limits on the y-axis from 0 to 200.


Notes and Thoughts

If we did not have the levels argument in the factor function then our bar graph would not have One, Two, Three, …, Six as desired. It would be alphabetically ordered. The following code and output will illustrate this.

Having results_data Counts = outcome_data) will fix the issue.


References

The following websites along with Datacamp courses have been very useful.

  • http://www.cookbook-r.com/Graphs/Bar_and_line_graphs_(ggplot2)/
  • http://docs.ggplot2.org/0.9.2.1/labs.html