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. Load the Chapter 8 workspace. It contains the necessary information for this chapter.
    > #load the chapter 8 workspace
    > load("rBeginnersGuide_Ch_08_ReadersCopy.RData")
    
  3. 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 <- mean(subsetFire$DurationInDays)
    > meanDurationAmbush <- mean(subsetAmbush$DurationInDays)
    > meanDurationHeadToHead <-
    mean(subsetHeadToHead$DurationInDays)
    > meanDurationSurround <- mean(subsetSurround$DurationInDays)
    > #use a vector to define the chart's bar values
    > barAllMethodsDurationBars <- c(meanDurationFire,
    meanDurationAmbush, meanDurationHeadToHead,
    meanDurationSurround)
    > #use barplot(...) to create and display the bar chart
    > barplot(height = barAllMethodsDurationBars)
    
  4. Your chart will be displayed in the graphic window, similar to the following:
    Time for action - creating a bar chart

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.

barplot(...)

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)

Vectors

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 <- c(1, 2, 3, 4, 5)

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 <- c("a", "b", "c", "d", "e")

Our vector defined the values for our bars:

> #use a vector to define the chart's bar values
> barAllMethodsDurationBars <- c(meanDurationFire,
meanDurationAmbush, meanDurationHeadToHead, meanDurationSurround)

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.

Graphic window

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

Graphic window

Pop quiz

  1. When entering text into a vector using the c(...) function, what characters must surround each text value?

    a. quotation marks

    b. parenthesis

    c. asterisks

    d. percent signs

  2. What is the purpose of the R graphic window?

    a. to debug graphics functions

    b. to execute graphics functions

    c. to edit graphics

    d. to display graphics

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.74.18