Agenda


  • create line plots
  • add color to lines
  • modify line type/style
  • modify line width
  • add points to the lines
  • modify axis range
  • add additional lines to the plot

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


plot(users$Date, users$Desktop, type = 'l', main = 'Daily Visitors',
     xlab = 'Date', ylab = 'Visitors')

Lines & Points


plot(users$Date, users$Desktop, type = 'b', main = 'Daily Visitors',
     xlab = 'Date', ylab = 'Visitors')

Overplotted


plot(users$Date, users$Desktop, type = 'o', main = 'Daily Visitors',
     xlab = 'Date', ylab = 'Visitors')

Lines with Breaks


plot(users$Date, users$Desktop, type = 'c', main = 'Daily Visitors',
     xlab = 'Date', ylab = 'Visitors')

Line Color


plot(users$Date, users$Desktop, type = 'l', col = 'blue', main = 'Daily Visitors',
     xlab = 'Date', ylab = 'Visitors')

Color


plot(users$Date, users$Desktop, type = 'b', col = 'blue', main = 'Daily Visitors',
     xlab = 'Date', ylab = 'Visitors')

Line Type


plot(users$Date, users$Desktop, type = 'l', lty = 3, main = 'Daily Visitors',
     xlab = 'Date', ylab = 'Visitors')

Line Types


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

Line Types


Line Types


Line Width


plot(users$Date, users$Desktop, type = 'l', lwd = 2.5,
     xlab = 'Date', ylab = 'Visitors')

Line Width


Enhance Points


plot(users$Date, users$Desktop, type = 'b', pch = 23, col = 'red', cex = 1.5, 
    main = 'Daily Visitors', xlab = 'Date', ylab = 'Visitors')

Add Points


plot(users$Date, users$Desktop, type = 'l', col = 'red', 
    main = 'Daily Visitors', xlab = 'Date', ylab = 'Visitors')
points(users$Date, users$Desktop, pch = 23, col = 'blue', bg = 'green', cex = 1.5)

Additional Lines


plot(users$Date, users$Desktop, type = "l", col = "blue", 
    main = 'Daily Visitors', xlab = 'Date', ylab = 'Visitors')
lines(users$Date, users$Mobile, type = "l", col = "red")
lines(users$Date, users$Tablet, type = "l", col = "green")

Additional Lines - Modify Axis Range


plot(users$Date, users$Desktop, type = "l", col = "blue", ylim = c(0, 3000), 
    main = 'Daily Visitors', xlab = 'Date', ylab = 'Visitors')
lines(users$Date, users$Mobile, type = "l", col = "red")
lines(users$Date, users$Tablet, type = "l", col = "green")

Putting it all together…


plot(users$Date, users$Desktop, type = "l", col = "blue", main = 'Daily Visitors',
     ylim = c(0, 3000), xlab = 'Date', ylab = 'Visitors')
lines(users$Date, users$Mobile, type = "l", col = "red")
lines(users$Date, users$Tablet, type = "l", col = "green")