18/12/2023

Overview

These slides are an examples of presentation made by R for the assignment of week 3 of the course Developing Data Products of Johns Hopkins University

Load the data

Load the data using the fread() function from data.table

data <- fread('data.csv', header = TRUE)
head(data, 5)
##               park    2009    2010    2011    2012    2013    2014    2015
## 1: Channel Islands  348745  277515  242756  249594  212029  342161  324816
## 2:    Death Valley  828574  984775  946867  984568  951972 1101312 1154843
## 3:     Joshua Tree 1304471 1434976 1396237 1396117 1383340 1589904 2025756
## 4:    Kings Canyon  609296  598205  566810  591033  567544  502268  468106
## 5: Lassen Volcanic  365639  384570  351269  407653  427409  432977  468092
##       2016    2017    2018
## 1:  364807  383687  366250
## 2: 1296283 1294827 1678660
## 3: 2505286 2853619 2942382
## 4:  607479  692932  699023
## 5:  536068  507256  499435

Convert the data

Convert the data from wide to long format and display data with two variables, year and visitors.

data <- data %>%
  gather(key = year, value = visitors, -park) %>%
  mutate(park = as.factor(park),
         year = as.factor(year))
head(data, 3)
##              park year visitors
## 1 Channel Islands 2009   348745
## 2    Death Valley 2009   828574
## 3     Joshua Tree 2009  1304471

Plot visitors

Plot the number of visitors (y) by year (x) as a line plot, with separate lines for each park using the plot_ly()

plot_ly(data = data, x = ~year, y = ~visitors, type = "scatter", 
        mode = "lines", color = ~park)

Visitors by year