To further explore line charts, we will experiment with modifying line widths and adding multiple custom lines to our graphics:
lwd
argument to set the line width:> #modify the chapter 8 single line chart that depicted the durations of past fire attacks > #use the lwd argument to set the line width > #lwd accepts a nonnegative value and defaults to 1 > lineFireDurationWidth <- 3 > #use plot(...) to create and display the line chart > #recall that a line chart uses the same plot(...) function as a scatterplot, but with a different type argument > plot(x = lineFireDurationDataX, y = lineFireDurationDataY, main = lineFireDurationMain, xlab = lineFireDurationLabX, ylab = lineFireDurationLabY, type = lineFireDurationType, lwd = lineFireDurationWidth)
> #prepare the line chart to incorporate data from the other battle methods > #modify the chart title > lineFireDurationMain = "Duration by Battle Method" > #rescale the y axis to handle the new data > lineFireDurationLimY <- c(0, 200) > #incorporate the col argument to distinguish between the different battle methods > lineFireDurationCol <- "red" > #use plot(...) to create and display the line chart > plot(x = lineFireDurationDataX, y = lineFireDurationDataY, main = lineFireDurationMain, xlab = lineFireDurationLabX, ylab = lineFireDurationLabY, ylim = lineFireDurationLimY, type = lineFireDurationType, lwd = lineFireDurationWidth, col = lineFireDurationCol)
lines(...)
function to add new relationships to the line chart:> #use lines(...) to add new relationships to a line chart > #add lines representing the three remaining battle methods to the chart > #note that after entering each subsequent function into the R console, it will be immediately drawn atop your existing line chart > #ambush > lineAmbushDataY <- subsetAmbush$DurationInDays > lineAmbushWidth <- 1 > lineAmbushCol <- "blue" > lines(x = lineFireDurationDataX, y = lineAmbushDataY, type = lineFireDurationType, lwd = lineAmbushWidth, col = lineAmbushCol) > #head to head > lineHeadToHeadDataY <- subsetHeadToHead$DurationInDays > lineHeadToHeadWidth <- 1 > lineHeadToHeadCol <- "darkorange2" > lines(x = lineFireDurationDataX, y = lineHeadToHeadDataY, type = lineFireDurationType, lwd = lineHeadToHeadWidth, col = lineHeadToHeadCol) > #surround > lineSurroundDataY <- subsetSurround$DurationInDays > lineSurroundWidth <- 1 > lineSurroundCol <- "forestgreen" > lines(x = lineFireDurationDataX, y = lineSurroundDataY, type = lineFireDurationType, lwd = lineSurroundWidth, col = lineSurroundCol)
> #add a legend to our line chart > #use the x and y arguments to specify the exact location of the legend > #add labels for the battle methods > #add fill colors to match the chart's lines > legend(x = 23, y = 210, legend = c("Fire", "Ambush", "Head to Head", "Surround"), fill = c("red", "blue", "darkorange2", "forestgreen"))
We expanded our use of the plot(...)
function to generate a line chart with a specific line width. Then, we worked to add additional lines to our chart for the purpose of portraying multiple relationships. We also incorporated a legend to make our chart more legible. Let us review these techniques.
We specified the width of our chart's line using the lwd
argument. This argument has a default value of 1 and can receive any number greater than zero. In most cases, you will want to use values between 1 and 3. Both our one-line and multiline charts used a lwd
value of 3 to emphasize the fire attack duration data by thickening its line.
> lineFireDurationWidth <- 3
The lwd
argument was seamlessly integrated into our plot(...)
function:
> plot(x = lineFireDurationDataX, y = lineFireDurationDataY, main = lineFireDurationMain, xlab = lineFireDurationLabX, ylab = lineFireDurationLabY, type = lineFireDurationType, lwd = lineFireDurationWidth)
Note that the lwd
argument can be used to modify the line thickness of data markers. For example, using a lwd
of 3 in a scatterplot would yield points with thicker markers. The lwd
argument can also be used within the abline(...)
function to alter a best fit line.
To add new relationships to our multiline chart, we employed the lines(...)
function. This function is used to draw additional lines on the chart that is displayed in the graphic window. The primary arguments of the lines(...)
function are:
Thus, the general format for the lines(...)
function is as follows:
lines(x = xPosition, y = yPosition, type = "type", col = "colorName")
After generating our chart with only fire attack data, we used lines(...)
to graph the duration values for our ambush, head to head, and surround methods. For these lines, we used a more subtle lwd
value of 1 and custom colors to differentiate them from one another.
> #ambush lines(...) functionexample> lineAmbushWidth <- 1 > lineAmbushCol <- "blue" > #head to head > lineHeadToHeadWidth <- 1 > lineHeadToHeadCol <- "darkorange2" > #surround > lineSurroundWidth <- 1 > lineSurroundCol <- "forestgreen"
A unique line(...)
function for battle method was executed to add its data to the line chart:
> #ambush > lines(x = lineFireDurationDataX, y = lineAmbushDataY, type = lineFireDurationType, lwd = lineAmbushWidth, col = lineAmbushCol) > #head to head > lines(x = lineFireDurationDataX, y = lineHeadToHeadDataY, type = lineFireDurationType, lwd = lineHeadToHeadWidth, col = lineHeadToHeadCol) > #surround > lines(x = lineFireDurationDataX, y = lineSurroundDataY, type = lineFireDurationType, lwd = lineSurroundWidth, col = lineSurroundCol)
Note that we also redefined the y-axis scale with ylim
prior to adding our new lines. This is necessary, because it allows all of our values to display within the bounds of our chart. If we did not rescale the y-axis, most of our points would fall outside the upper limit of our graph. This is because the fire attack duration values are much smaller than in our other battle methods.
When adding new relationships to a graphic, remember to adjust your axes accordingly to ensure that all data are represented.
Once again, we added a legend to our chart in order to identify each line. We used the already familiar legend(...)
function to do so, making sure to match the legend's title and colors to those of the lines on our chart:
> legend(x = 23, y = 210, legend = c("Fire", "Ambush", "Head to Head", "Surround"), fill = c("red", "blue", "darkorange2", "forestgreen"))
plot(...)
function, which of the following is not true of the lwd
argument?a. One or more of a chart's lines can have a unique lwd
value.
b. The lwd
argument defaults to a value of 1.
c. The lwd
argument accepts a nonnegative numeric value.
d. To take effect, the lty
argument must be defined.
lines(...)
function to add new lines to a chart, which of the following is not a true statement?a. One or more lines can be added to a single chart.
b. The widths of a chart's lines can be different.
c. To display a new line, the chart's data must be in matrix form.
d. The x or y axis may need to be rescaled to properly portray a new line.
Create a multiline chart that portrays the number of Shu soldiers engaged in all instances of each battle method. You should have a line for each battle method. Be sure to experiment with the type
and lwd
arguments, as well as the lines(...)
function, to witness the different line chart styles that can be generated in R. Once your graph is complete, remember to add a legend that identifies each line.