Time for action - creating a line chart

The ever popular line chart, or line graph, depicts relationships as continuous series of connected data points. Line charts are particularly useful for visualizing specific values and trends over time. Just as a line chart is an extension of a scatterplot in the non-digital realm, a line chart is created using an extended form of the plot(...) function in R. Let us explore how to extend the plot(...) function to create line charts in R:

  1. Use the type argument within the plot(...) function to create a line chart that depicts a single relationship between two variables:
    > #create a line chart that depicts the durations of past fire attacks
    > #get the data to be used in the chart
    > lineFireDurationDataX <- c(1:30)
    > lineFireDurationDataY <- subsetFire$DurationInDays
    > #customize the chart
    > lineFireDurationMain <- "Duration of Past Fire Attacks"
    > lineFireDurationLabX <- "Battle Number"
    > lineFireDurationLabY <- "Duration in Days"
    > #use the type argument to connect the data points with a line
    > lineFireDurationType <- "o"
    > #use plot(...) to create and display the line chart
    > plot(x = lineFireDurationDataX, y = lineFireDurationDataY,
    main = lineFireDurationMain, xlab = lineFireDurationLabX,
    ylab = lineFireDurationLabY, type = lineFireDurationType)
    
  2. Your chart will be displayed in the graphic window, as follows:
    Time for action - creating a line chart

What just happened?

We expanded our use of the plot(...) function to generate a line chart and encountered a new data notation in the process. Let us review these features.

type

In the plot(...) function, the type argument determines what kind of line, if any, should be used to connect a chart's data points. The type argument receives one of several character values, all of which are listed as follows:

  • p: only points are plotted; this is the default value when type is undefined
  • l: only lines are drawn, without any points
  • o: both lines and points are drawn, with the lines overlapping the points
  • b: both lines and points are drawn, with the lines broken where they intersect with points
  • c: only lines are drawn, but they are broken where points would occur
  • s: only the lines are drawn in step formation; the initial step begins at zero
  • S: (uppercase) only the lines are drawn in step formation; the final step tails off at the last point
  • h: vertical lines are drawn to represent each point
  • n: no points nor lines are drawn

Our chart, which represented the duration of past fire attacks, featured a line that overlapped the plotted points. First, we defined our desired line type in an R variable:

> lineFireDurationType <- "o"

Then the type argument was placed within our plot(...) function to generate the line chart:

> plot(lineFireDurationDataX, lineFireDurationDataY,
main = lineFireDurationMain, xlab = lineFireDurationLabX,
ylab = lineFireDurationLabY,
type = lineFireDurationType)

Number-colon-number notation

You may have noticed that we specified a vector for the x-axis data in our plot(...) function.

> lineFireDurationDataX <- c(1:30)

This vector used number-colon-number notation. Essentially, this notation has the effect of enumerating a range of values that lie between the number that precedes the colon and the number that follows it. To do so, it adds one to the beginning value until it reaches a final value that is equal to or less than the number that comes after the colon. For example, the code> 14:21 would yield eight whole numbers, beginning with 14 and ending with 21, as follows:

[1] 14 15 16 17 18 19 20 21

Furthermore, the code> 14.2:21 would yield seven values, beginning with 14.2 and ending with 20.2, as follows:

[1] 14.2 15.2 16.2 17.2 18.2 19.2 20.2

Number-colon-number notation is a useful way to enumerate a series of values without having to type each one individually. It can be used in any circumstance where a series of values is acceptable input into an R function.

Note

Number-colon-number notation can also enumerate values from high to low. For instance, 21:14 would yield a list of values beginning with 21 and ending with 14.

Since we do not have exact dates or other identifying information for our 30 past battles, we simply enumerated the numbers 1 through 30 on the x-axis. This had the effect of assigning a generic identification number to each of our past battles, which in turn allowed us to plot the duration of each battle on the y axis.

Pop quiz

  1. Which of the following is the type argument capable of?

    a. Drawing a line to connect or replace the points on a scatterplot.

    b. Drawing vertical or step lines.

    c. Drawing no points or lines.

    d. All of the above.

  2. What would the following line of code yield in the R console?
    > 1:50
    

    a. A sequence of 50 whole numbers, in order from 1 to 50.

    b. A sequence of 50 whole numbers, in order from 50 to 1.

    c. A sequence of 50 random numbers, in order from 1 to 50.

    d. A sequence of 50 random numbers, in order from 50 to 1.

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

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