Plotting in R with graphics

Introduction

The graphics package is included with the language, meaning you won’t need to import anything at the beginning of your file. It includes a ton of useful, variably-complex plots to use on your journey of data visualization.

Examples

Use the sapply function to run this function on each month to get total amount of money spent on taxi cab rides each day. Use the tapply function to add up the amounts spent per day. Plot the total amount of money spent on taxi cab rides during each day in 2018.

myfares <- function(mymonth) {
    myDF <- fread(paste0("/anvil/projects/tdm/data/taxi/yellow/yellow_tripdata_2018-", mymonth, ".csv"), select=c(2,17))
    mytable <- tapply(myDF$total_amount, as.Date(myDF$tpep_pickup_datetime), sum)
    return(mytable)
}
Click to see solution
myfares <- function(mymonth) {
    myDF <- fread(paste0("/anvil/projects/tdm/data/taxi/yellow/yellow_tripdata_2018-", mymonth, ".csv"), select=c(2,17))
    mytable <- tapply(myDF$total_amount, as.Date(myDF$tpep_pickup_datetime), sum)
    return(mytable)
}

library(data.table)
myresults <- sapply( sprintf("%02d", 1:12), myfares )

names(myresults) <- NULL
v <- do.call(c, myresults)
mytotals <- tapply(v, names(v), sum)
betterdates <- mytotals[year(as.Date(names(mytotals))) == 2018]
plot( as.Date(names(betterdates)), betterdates )