4 min read

Statistical Analysis with R

Statistical Analysis with R

Take control of your data and produce superior statistical analysis with R.

  • An easy introduction for people who are new to R, with plenty of strong examples for you to work through
  • This book will take you on a journey to learn R as the strategist for an ancient Chinese kingdom!
  • A step by step guide to understand R, its benefits, and how to use it to maximize the impact of your data analysis
  • A practical guide to conduct and communicate your data analysis with R in the most effective manner      

Charts, graphs, and plots in R

R features several options for creating charts, graphs, and plots. In this article, we will explore the generation and customization of these visuals, as well as methods for saving and exporting them for use outside of R. The following visuals will be covered in this article:

  • Bar graphs
  • Scatterplots
  • Line charts
  • Box plots
  • Histograms
  • Pie charts

Time for action — creating a bar chart

A bar chart or bar graph is a common visual that uses rectangles to depict the values of different items. Bar graphs are especially useful when comparing data over time or between diverse groups. Let us create a bar chart in R:

  1. Open R and set your working directory:

    > #set the R working directory
    > #replace the sample location with one that is relevant to you
    > setwd(“/Users/johnmquick/rBeginnersGuide/”)

  2. Use the barplot(…) function to create a bar chart:

    > #create a bar chart that compares the mean durations of
    the battle methods
    > #calculate the mean duration of each battle method
    > meanDurationFire > meanDurationAmbush > meanDurationHeadToHead mean(subsetHeadToHead$DurationInDays)
    > meanDurationSurround > #use a vector to define the chart’s bar values
    > barAllMethodsDurationBars meanDurationAmbush, meanDurationHeadToHead,
    > #use barplot(…) to create and display the bar chart
    > barplot(height = barAllMethodsDurationBars)

  3. Your chart will be displayed in the graphic window, similar to the following:

What just happened?

You created your first graphic in R. Let us examine the barplot(…) function that we used to generate our bar chart, along with the new R components that we encountered.


We created a bar chart that compared the mean durations of battles between the different combat methods. As it turns out, there is only one required argument in the barplot(…) function. This height argument receives a series of values that specify the length of each bar. Therefore, the barplot(…) function, at its simplest, takes on the following form:

barplot(height = heightValues)

Accordingly, our bar chart function reflected this same format:

> barplot(height = barAllMethodsDurationBars)


We stored the heights of our chart’s bars in a vector variable. In R, a vector is a series of data. R’s c(…) function can be used to create a vector from one or more data points. For example, the numbers 1, 2, 3, 4, and 5 can be arranged into a vector like so:

> #arrange the numbers 1, 2, 3, 4, and 5 into a vector
> numberVector

Similarly, text data can also be placed into vector form, so long as the values are contained within quotation marks:

> #arrange the letters a, b, c, d, and e into a vector
> textVector

Our vector defined the values for our bars:

> #use a vector to define the chart’s bar values
> barAllMethodsDurationBars

Many function arguments in R require vector input. Hence, it is very common to use and encounter the c(…) function when working in R.

Graphic window

When you executed your barplot(…) function in the R console, the graphic window opened to display it. The graphic window will have different names across different operating systems, but its purpose and function remain the same. For example, in Mac OS X, the graphic window is named Quartz.

For the remainder of this article, all R graphics will be displayed without the graphics window frame, which will allow us to focus on the visuals themselves.

Pop quiz

    1. When entering text into a vector using the c(…) function, what characters must surround each text value?
      1. Quotation marks
      2. Parenthesis
      3. Asterisks
      4. Percent signs


  1. What is the purpose of the R graphic window?
    1. To debug graphics functions
    2. To execute graphics functions
    3. To edit graphics
    4. To display graphics

Subscribe to the weekly Packt Hub newsletter. We'll send you this year's Skill Up Developer Skills Report.

* indicates required


Please enter your comment!
Please enter your name here