Visualizing Time Series
- sam33frodon
- Jan 27, 2021
- 2 min read
Updated: Feb 1, 2021
library(ggplot2)
library(readr)
library(tidyverse)
The data is available on the following website : https://data.neonscience.org/
data <- read_csv("Met_HARV_Daily_2009_2011.csv")
str(harMetDaily.09.11$date)
## Date[1:1095], format: "2009-01-01" "2009-01-02" "2009-01-03" "2009-01-04" "2009-01-05" ...
harMetDaily.09.11 %>%
select(date, airt) %>%
head(10)
## # A tibble: 10 x 2
## date airt
## <date> <dbl>
## 1 2009-01-01 -15.1
## 2 2009-01-02 -9.1
## 3 2009-01-03 -5.5
## 4 2009-01-04 -6.4
## 5 2009-01-05 -2.4
## 6 2009-01-06 -4.9
## 7 2009-01-07 -2.6
## 8 2009-01-08 -3.2
## 9 2009-01-09 -9.9
## 10 2009-01-10 -11.1
df <- harMetDaily.09.11 %>%
select(date, airt)
ggplot(df, aes(date, airt)) +
geom_point(color = "purple",
size = 3,
pch = 18) +
labs(x= "Date",
y= expression(paste("Temperature ( ", degree ~ C, " )")),
title = "Air Temperature (2009-2011)\nNEON Harvard Forest Field Site") +
scale_x_date(labels = date_format("%b %y")) +
stat_smooth(colour = "green") +
theme(plot.title = element_text(color="darkblue", size=14, face="bold.italic"),
text = element_text(size=14,face="bold"),
axis.title.x = element_text(size=14, face="bold"),
axis.title.y = element_text(size=14, face="bold"))

APPLICATION
1. Number of crimes in Toronto
Torontocrimes <- read_csv("Torontocrimes.csv")
str(Torontocrimes$occurrencedate)
## POSIXct[1:166500], format: "2014-06-20 10:55:00" "2014-07-02 00:20:00" "2014-07-02 00:20:00" ...
Torontocrimes %>%
filter(occurrenceyear == 2018) %>%
select(occurrencedate) %>%
mutate(date = as.Date(occurrencedate)) %>%
group_by(date) %>%
summarise(frequency = n()) %>%
head(10)
## # A tibble: 10 x 2
## date frequency
## <date> <int>
## 1 2018-01-01 177
## 2 2018-01-02 83
## 3 2018-01-03 67
## 4 2018-01-04 72
## 5 2018-01-05 100
## 6 2018-01-06 89
## 7 2018-01-07 90
## 8 2018-01-08 119
## 9 2018-01-09 87
## 10 2018-01-10 90
nf <- Torontocrimes %>%
filter(occurrenceyear == 2018) %>%
select(occurrencedate) %>%
mutate(date = as.Date(occurrencedate)) %>%
group_by(date) %>%
summarise(frequency = n())
ggplot(nf, aes(date, frequency)) +
geom_line() +
theme(plot.title = element_text(color="darkblue", size=14, face="bold.italic"),
text = element_text(size=14,face="bold"),
axis.title.x = element_text(size=14, face="bold"),
axis.title.y = element_text(size=14, face="bold"))

library(ggTimeSeries)
Torontocrimes %>%
select(occurrencedate) %>%
mutate(date = as.Date(occurrencedate)) %>%
group_by(date) %>%
summarise(frequency = n()) %>%
ggplot_calendar_heatmap("date", "frequency") +
xlab(NULL) +
ylab(NULL) +
facet_wrap(~Year, ncol = 1) +
scale_fill_gradient(high="red", low="yellow")+
theme(plot.title = element_text(color="darkblue", size=14, face="bold.italic"),
text = element_text(size=14,face="bold"),
axis.title.x = element_text(size=14, face="bold"),
axis.title.y = element_text(size=14, face="bold"))

library(gapminder)
gapminder %>%
filter(country == "Canada") %>%
select(year, lifeExp) %>%
ggplot(aes(x=year, y=lifeExp)) +
geom_line()+
geom_point(size = 4, color = "blue")+
labs(x = NULL, y = NULL) +
theme(text = element_text(size = 16),
panel.border = element_rect(fill = NA, colour = "grey20")) +
labs(title = "Life expectancy in Canada",
x = NULL,
y = "Life expectancy (Years)") +
theme_minimal() +
theme(plot.title = element_text(color="darkblue", size=14, face="bold.italic"),
text = element_text(size=14,face="bold"),
axis.title.x = element_text(size=14, face="bold"),
axis.title.y = element_text(size=14, face="bold"))

Comentários