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 year from 1987 to 2008. Then, plot the total number of flights starting from the Indianapolis airport during 1987 to 2008.
myindyflights <- function(myyear) {
myDF <- fread(paste0("/anvil/projects/tdm/data/flights/subset/", myyear, ".csv"))
myvalue <- table(myDF$Origin)['IND']
names(myvalue) <- myyear
return(myvalue)
}
Click to see solution
myindyflights <- function(myyear) {
myDF <- fread(paste0("/anvil/projects/tdm/data/flights/subset/", myyear, ".csv"))
myvalue <- table(myDF$Origin)['IND']
names(myvalue) <- myyear
return(myvalue)
}
library(data.table)
myresults <- sapply(1987:2008, myindyflights)
plot(1987:2008, myresults)
Plot the total number of flights starting from each of the top 10 airports during 1987 to 2008.
myflights <- function(myyear) {
myDF <- fread(paste0("/anvil/projects/tdm/data/flights/subset/", myyear, ".csv"))
myvalue <- table(myDF$Origin)
return(myvalue)
}
Click to see solution
myflights <- function(myyear) {
myDF <- fread(paste0("/anvil/projects/tdm/data/flights/subset/", myyear, ".csv"))
myvalue <- table(myDF$Origin)
return(myvalue)
}
library(data.table)
myresults <- sapply(1987:2008, myflights)
v <- unlist(myresults)
dotchart(tail(sort(tapply(v, names(v), sum)), n=10))