Line graphs show change over an ordered axis, usually time; slope is the message.
WarningCommon pitfall
Unsorted or gappy x values draw zigzag lines; sort by x and handle missing periods explicitly.
In this chapter, we will learn to:
build
simple line chart
grouped line chart
map aesthetics to variables
modify line
color
type
size
7.2 Case Study
We will use a data set related to GDP growth rate. You can download it from here. It contains GDP (Gross Domestic Product) growth data for the BRICS (Brazil, Russia, India, China, South Africa) for the years 2000 to 2005.
7.2.1 Data
gdp <- readr::read_csv('https://raw.githubusercontent.com/rsquaredacademy/datasets/master/gdp.csv')gdp
To create a line chart, use geom_line(). In the below example, we examine the GDP growth rate trend of India for the years 2000 to 2005.
ggplot(gdp, aes(year, india)) +geom_line()
7.3.1 Line Color
To modify the color of the line, use the color argument and supply it a valid color name. In the below example, we modify the color of the line to 'blue'. Remember that the color argument should be outside aes().
The line type can be modified using the linetype argument. It can take 7 different values. You can specify the line type either using numbers or words as shown below:
0 : blank
1 : solid
2 : dashed
3 : dotted
4 : dotdash
5 : longdash
6 : twodash
Let us modify the line type to dashed style by supplying the value 2 to the linetype argument.
Now let us map the aesthetics to the variables. The data used in the above example cannot be used as we need a variable with country names. We will use pivot_longer() function from the tidyr package to reshape the data.
gdp2 <- gdp |>select(year, growth, india, china) |> tidyr::pivot_longer(cols =-year, names_to ="country", values_to ="gdp")gdp2
# A tibble: 18 × 3
year country gdp
<date> <chr> <dbl>
1 2000-01-01 growth 6
2 2000-01-01 india 5
3 2000-01-01 china 8
4 2001-01-01 growth 9
5 2001-01-01 india 9
6 2001-01-01 china 5
7 2002-01-01 growth 8
8 2002-01-01 india 8
9 2002-01-01 china 6
10 2003-01-01 growth 9
11 2003-01-01 india 8
12 2003-01-01 china 8
13 2004-01-01 growth 9
14 2004-01-01 india 5
15 2004-01-01 china 9
16 2005-01-01 growth 8
17 2005-01-01 india 7
18 2005-01-01 china 8
In the original data, to plot GDP trend of multiple countries we will have to use geom_line() multiple times. But in the reshaped data, we have the country names as one of the variables and this can be used along with the group argument to plot data of multiple countries with a single line of code as shown below. By mapping country to the group argument, we have plotted data of all countries.
ggplot(gdp2, aes(year, gdp, group = country)) +geom_line()
In the above plot, we cannot distinguish between the lines and there is no way to identify which line represents which country. To make it easier to identify the trend of different countries, let us map the color argument to the variable country as shown below. Now, each country will be represented by line of different color.
ggplot(gdp2, aes(year, gdp, group = country)) +geom_line(aes(color = country))
We can map linetype argument to country as well. In this case, each country will be represented by a different line type.
ggplot(gdp2, aes(year, gdp, group = country)) +geom_line(aes(linetype = country))
We can map the width of the line to the variable country as well. But in this case, the plot does not look either elegant or intuitive.
ggplot(gdp2, aes(year, gdp, group = country)) +geom_line(aes(linewidth = country))
Remember that in all the above cases, we mapped the arguments to a variable inside aes().
7.5 Exercises
Draw the unemploy series from economics over date with linewidth = 1.
Reshape unemploy + uempmed to long format and draw both series distinguished by color.
Rewrite the plot with a message-first title and units on the y axis.
Worked solutions in solutions/line.md (attempt first).
Where to go next
ggplot2 book (ggplot2-book.org) for theory behind this layer
Claus Wilke, Fundamentals of Data Visualization, for perception guidance