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

Plot

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

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

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

Example

ggplot(gdp, aes(year, india)) +
geom_line(linetype = 2) 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

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

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

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

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

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))