Line chart

Line charts are most often used to indicate change, particularly over time. This time, we will use the longley dataset, featuring economic variables from between 1947 and 1962, as shown in the following code:

> plot(x = 1947 : 1962, y = longley$GNP, type = "l",
  xlab = "Year", main = "Base graphics")

The x axis is given very simply by the 1947 : 1962 phrase, which enumerates all the numbers between 1947 and 1962, and the type = "l" argument specifies the plotting of the lines, as opposed to points or both.

The ggplot call looks a lot like the bar chart, except with an x and y dimension in the aesthetics this time. The command looks as follows:

> ggplot(longley, aes(x = 1947 : 1962, y = GNP)) + geom_line() +
     xlab("Year") + ggtitle("ggplot2") ...

Get Web Application Development with R Using Shiny - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.