Agenda


Learn to add legend to a plot using the legend() function. Specifically

  • position the legend within the plot
  • modify the layout using ncol and horiz arguments
  • add title using the title. set of arguments
  • modify the appearance and position of the legend box
  • modify the appearance of the text in the legend box

Introduction


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 Graph


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

Legend Location


  • use x and y axis coordinates
  • use keywords
    • top
    • bottom
    • left
    • right
    • center
    • bottomright
    • bottomleft
    • topright
    • topleft

Legend Location


{plot(users$Date, users$Desktop, type = "l", col = "blue", main = 'Daily Visitors',
     ylim = c(0, 4000), xlab = 'Date', ylab = 'Visitors')
lines(users$Date, users$Mobile, type = "l", col = "red")
lines(users$Date, users$Tablet, type = "l", col = "green")  
legend(x = as.Date('2017-10-13'), y = 3900, legend = c('Desktop', 'Mobile', 'Tablet'))}

Legend Location


Lines


{plot(users$Date, users$Desktop, type = "l", col = "blue", main = 'Daily Visitors',
     ylim = c(0, 4000), xlab = 'Date', ylab = 'Visitors')
lines(users$Date, users$Mobile, type = "l", col = "red")
lines(users$Date, users$Tablet, type = "l", col = "green")  
legend(x = as.Date('2017-10-13'), y = 3900, legend = c('Desktop', 'Mobile', 'Tablet'), lty = 1, 
       col = c('blue', 'red', 'green'))}

Points


{plot(users$Date, users$Desktop, type = "l", col = "blue", main = 'Daily Visitors',
     ylim = c(0, 4000), xlab = 'Date', ylab = 'Visitors')
points(users$Date, users$Desktop, pch = 2, col = 'blue')
lines(users$Date, users$Mobile, type = "l", col = "red")
points(users$Date, users$Mobile, pch = 2, col = 'red')
lines(users$Date, users$Tablet, type = "l", col = "green")
points(users$Date, users$Tablet, pch = 2, col = 'green')  
legend(x = as.Date('2017-10-13'), y = 3900, legend = c('Desktop', 'Mobile', 'Tablet'), lty = 1, 
       col = c('blue', 'red', 'green'), pch = 2)}

Text Placement


{plot(users$Date, users$Desktop, type = "l", col = "blue", main = 'Daily Visitors',
     ylim = c(0, 4000), xlab = 'Date', ylab = 'Visitors')
lines(users$Date, users$Mobile, type = "l", col = "red")
lines(users$Date, users$Tablet, type = "l", col = "green")  
legend(x = as.Date('2017-10-13'), y = 3900, legend = c('Desktop', 'Mobile', 'Tablet'), lty = 1, 
       col = c('blue', 'red', 'green'), horiz = TRUE)}

Text Placement


{plot(users$Date, users$Desktop, type = "l", col = "blue", main = 'Daily Visitors',
     ylim = c(0, 4000), xlab = 'Date', ylab = 'Visitors')
lines(users$Date, users$Mobile, type = "l", col = "red")
lines(users$Date, users$Tablet, type = "l", col = "green")  
legend(x = as.Date('2017-10-13'), y = 3900, legend = c('Desktop', 'Mobile', 'Tablet'), lty = 1, 
       col = c('blue', 'red', 'green'), ncol = 2)}

Text Placement


Title


{plot(users$Date, users$Desktop, type = "l", col = "blue", main = 'Daily Visitors',
     ylim = c(0, 4000), xlab = 'Date', ylab = 'Visitors')
lines(users$Date, users$Mobile, type = "l", col = "red")
lines(users$Date, users$Tablet, type = "l", col = "green")  
legend(x = as.Date('2017-10-13'), y = 3900, legend = c('Desktop', 'Mobile', 'Tablet'), lty = 1, 
       col = c('blue', 'red', 'green'), title = 'Devices', horiz = TRUE)}

Title Color


{plot(users$Date, users$Desktop, type = "l", col = "blue", main = 'Daily Visitors',
     ylim = c(0, 4000), xlab = 'Date', ylab = 'Visitors')
lines(users$Date, users$Mobile, type = "l", col = "red")
lines(users$Date, users$Tablet, type = "l", col = "green")  
legend(x = as.Date('2017-10-13'), y = 3900, legend = c('Desktop', 'Mobile', 'Tablet'), lty = 1, 
       col = c('blue', 'red', 'green'), title = 'Devices', title.col = 'red', horiz = TRUE)}

Title Position


{plot(users$Date, users$Desktop, type = "l", col = "blue", main = 'Daily Visitors',
     ylim = c(0, 4000), xlab = 'Date', ylab = 'Visitors')
lines(users$Date, users$Mobile, type = "l", col = "red")
lines(users$Date, users$Tablet, type = "l", col = "green")  
legend(x = as.Date('2017-10-13'), y = 3900, legend = c('Desktop', 'Mobile', 'Tablet'), lty = 1, 
       col = c('blue', 'red', 'green'), title = 'Devices', title.adj = 0.1, horiz = TRUE)}

Title Position


Box Appearance


option argument values
Box Type bty o, n
Background Color bg blue, #0000ff
Border Line Type box.lty 1:5
Border Line Width box.lwd 0.5, 1, 1.5
Border Line Color box.col blue, #0000ff
Horizontal Justification xjust 0:1
Vertical Justification yjust 0:1
Text Color text.col blue, #0000ff
Text Font text.font 1:5

Box Type


Background Color


{plot(users$Date, users$Desktop, type = "l", col = "blue", main = 'Daily Visitors',
     ylim = c(0, 4000), xlab = 'Date', ylab = 'Visitors')
lines(users$Date, users$Mobile, type = "l", col = "red")
lines(users$Date, users$Tablet, type = "l", col = "green")  
legend(x = as.Date('2017-10-13'), y = 3900, legend = c('Desktop', 'Mobile', 'Tablet'), lty = 1, 
       col = c('blue', 'red', 'green'), title = 'Devices', bg = '#ffff66', horiz = TRUE)}

Border


The following arguments can be used to modify the border of the legend box:

  • box.lty: line type
  • box.lwd: line width
  • box.col: color

Border


{plot(users$Date, users$Desktop, type = "l", col = "blue", main = 'Daily Visitors',
     ylim = c(0, 4000), xlab = 'Date', ylab = 'Visitors')
lines(users$Date, users$Mobile, type = "l", col = "red")
lines(users$Date, users$Tablet, type = "l", col = "green")  
legend(x = as.Date('2017-10-13'), y = 3900, legend = c('Desktop', 'Mobile', 'Tablet'), lty = 1, 
       col = c('blue', 'red', 'green'), title = 'Devices', horiz = TRUE,
       box.lty = 3, box.lwd = 1.5, box.col = 'green')}

Justification


The xjust and yjust arguments can be used to position the legend relative to the X and Y axis respectively. Listed below is the value and the respective justification:

  • 0: left justified
  • 0.5: centered
  • 1: right justified

Horizontal Justification


Vertical Justification


Text Appearance


{plot(users$Date, users$Desktop, type = "l", col = "blue", main = 'Daily Visitors',
     ylim = c(0, 4000), xlab = 'Date', ylab = 'Visitors')
lines(users$Date, users$Mobile, type = "l", col = "red")
lines(users$Date, users$Tablet, type = "l", col = "green")  
legend(x = as.Date('2017-10-13'), y = 3900, legend = c('Desktop', 'Mobile', 'Tablet'), lty = 1, 
       col = c('blue', 'red', 'green'), title = 'Devices', 
       text.col = 'green', text.font = 3, horiz = TRUE)}