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:
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)
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.
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 undefinedl:
only lines are drawn, without any pointso:
both lines and points are drawn, with the lines overlapping the pointsb:
both lines and points are drawn, with the lines broken where they intersect with pointsc:
only lines are drawn, but they are broken where points would occurs:
only the lines are drawn in step formation; the initial step begins at zeroS:
(uppercase) only the lines are drawn in step formation; the final step tails off at the last pointh:
vertical lines are drawn to represent each pointn:
no points nor lines are drawnOur 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)
You may have noticed that we specified a vector for the x-axis data in our plot(...)
function.
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.
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.
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.
> 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.