Line Graph

Introduction


Line charts are used to examine trend over time.

Agenda


  • build
    • simple line chart
    • grouped line chart
  • map aesthetics to variables
  • modify line
    • color
    • type
    • size

Case Study

Introduction


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.

Data


gdp

Data Dictionary

Plot

Introduction


Example


ggplot(gdp, aes(year, india)) +
  geom_line()

Instructions


ggplot(gdp, aes(year, india)) +
  geom_line()
ggplot(gdp, aes(year, india)) +
  geom_line()

Line Color

Introduction


Example


ggplot(gdp, aes(year, india)) +
  geom_line(color = 'blue')

Instructions


ggplot(gdp, aes(year, india)) +
  geom_line(color = 'blue')
ggplot(gdp, aes(year, india)) +
  geom_line(color = 'blue')

Line Type - Part 1

Introduction


  • 0 : blank
  • 1 : solid
  • 2 : dashed
  • 3 : dotted
  • 4 : dotdash
  • 5 : longdash
  • 6 : twodash

Example


ggplot(gdp, aes(year, india)) +
  geom_line(linetype = 2)

Line Type - Part 2

Introduction


Example


ggplot(gdp, aes(year, india)) +
  geom_line(linetype = 'dashed')

Instructions


ggplot(gdp, aes(year, india)) +
  geom_line(linetype = 'dashed')
ggplot(gdp, aes(year, india)) +
  geom_line(linetype = 'dashed')

Line Size

Introduction


Example


ggplot(gdp, aes(year, india)) +
  geom_line(size = 2)

Instructions


ggplot(gdp, aes(year, india)) +
  geom_line(size = 2)
ggplot(gdp, aes(year, india)) +
  geom_line(size = 2)

Reshape Data

Introduction


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 gather() function from the tidyr package to reshape the data.

gdp2

Grouped Line Chart

Introduction


Example


ggplot(gdp2, aes(year, gdp, group = country)) +
  geom_line()

Instructions


ggplot(gdp2, aes(year, gdp, group = country)) +
  geom_line()
ggplot(gdp2, aes(year, gdp, group = country)) +
  geom_line()

Map Color

Introduction


Example


ggplot(gdp2, aes(year, gdp, group = country)) +
  geom_line(aes(color = country))

Instructions


ggplot(gdp2, aes(year, gdp, group = country)) +
  geom_line(aes(color = country))
ggplot(gdp2, aes(year, gdp, group = country)) +
  geom_line(aes(color = country))

Map Line Type

Introduction


Example


ggplot(gdp2, aes(year, gdp, group = country)) +
  geom_line(aes(linetype = country))

Instructions


ggplot(gdp2, aes(year, gdp, group = country)) +
  geom_line(aes(linetype = country))
ggplot(gdp2, aes(year, gdp, group = country)) +
  geom_line(aes(linetype = country))

Map Line Width

Introduction


Example


ggplot(gdp2, aes(year, gdp, group = country)) +
  geom_line(aes(size = country))
## Warning: Using size for a discrete variable is not advised.

Instructions


ggplot(gdp2, aes(year, gdp, group = country)) +
  geom_line(aes(size = country))
ggplot(gdp2, aes(year, gdp, group = country)) +
  geom_line(aes(size = country))