Agenda


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

Libraries


library(ggplot2)
library(readr)

Data


users <- readr::read_csv('https://raw.githubusercontent.com/rsquaredacademy/datasets/master/users_device.csv',
  col_types = list(col_date(format = "%m/%d/%y"), col_integer(),
                   col_integer(), col_integer()))
## # A tibble: 33 x 4
##          Date Desktop Mobile Tablet
##        <date>   <int>  <int>  <int>
##  1 2017-10-11    2345    876     92
##  2 2017-10-12    2173    784    111
##  3 2017-10-13    1826    772     97
##  4 2017-10-14    1178   1032    155
##  5 2017-10-15    1239    747    105
##  6 2017-10-16    2158    801     85
##  7 2017-10-17    2682   1307    127
##  8 2017-10-18    2252   1110    112
##  9 2017-10-19    2210    891     93
## 10 2017-10-20    2040    824     94
## # ... with 23 more rows

Data Dictionary


Below is the description of the data set:

  • Date: date
  • Desktop: visitors from a desktop device
  • Mobile: visitors from a mobile device
  • Tablet: visitors from a tablet device

Line Chart


ggplot(users, aes(Date, Desktop)) +
  geom_line()

Line Color


ggplot(users, aes(Date, Desktop)) +
  geom_line(color = 'blue')

Line Type


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

Line Type


ggplot(users, aes(Date, Desktop)) +
  geom_line(linetype = 2)

Line Type (Dashed)


ggplot(users, aes(Date, Desktop)) +
  geom_line(linetype = 'dashed')

Line Size


ggplot(users, aes(Date, Desktop)) +
  geom_line(size = 2)

Additional Lines


ggplot(users) +
  geom_line(aes(Date, Desktop)) +
  geom_line(aes(Date, Mobile)) +
  geom_line(aes(Date, Tablet)) 

Data


tidy_users <- readr::read_csv('https://raw.githubusercontent.com/rsquaredacademy/datasets/master/tidy_users.csv',
  col_types = list(col_date(format = "%m/%d/%y"), col_character(), col_integer()))
## # A tibble: 99 x 3
##          Date  Device Users
##        <date>   <chr> <int>
##  1 2017-10-11 Desktop  2345
##  2 2017-10-11  Mobile   876
##  3 2017-10-11  Tablet    92
##  4 2017-10-12 Desktop  2173
##  5 2017-10-12  Mobile   784
##  6 2017-10-12  Tablet   111
##  7 2017-10-13 Desktop  1826
##  8 2017-10-13  Mobile   772
##  9 2017-10-13  Tablet    97
## 10 2017-10-14 Desktop  1178
## # ... with 89 more rows

Data Dictionary


Below is the description of the data set:

  • Date: date
  • Device: device used by visitors
  • Users: total daily visitors

Grouped Line Chart


ggplot(tidy_users, aes(Date, Users, group = Device)) +
  geom_line()

Map Color to Device


ggplot(tidy_users, aes(Date, Users, group = Device)) +
  geom_line(aes(color = Device))

Map Line Type to Device


ggplot(tidy_users, aes(Date, Users, group = Device)) +
  geom_line(aes(linetype = Device))

Map Line Width to Device


ggplot(tidy_users, aes(Date, Users, group = Device)) +
  geom_line(aes(size = Device))
## Warning: Using size for a discrete variable is not advised.