#11.1
library(shiny)
library(datetime)
## Warning: package 'datetime' was built under R version 3.6.2
library(macleish)
## Warning: package 'macleish' was built under R version 3.6.2
## Loading required package: etl
## Warning: package 'etl' was built under R version 3.6.2
## Loading required package: dplyr
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyverse)
## -- Attaching packages --------------------------------------------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 3.2.1 v readr 1.3.1
## v tibble 2.1.3 v purrr 0.3.2
## v tidyr 1.0.0 v stringr 1.4.0
## v ggplot2 3.2.1 v forcats 0.4.0
## -- Conflicts ------------------------------------------------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(ggplot2)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
tail(whately_2015)
whatly_2015plot= whately_2015 %>% ggplot(aes(x=when, y=temperature)) +
geom_line(color="darkgrey",size=1)+
geom_smooth()+
xlab('When')+
ylab('Temperature (degrees Fahrenheit)')
whatly_2015plot
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
subset(whately_2015$temperature, whately_2015$when == '2015-03-20 22:50:00')
## [1] -4.654
subset(whately_2015$temperature, whately_2015$when == '2015-09-23 08:20:00')
## [1] 24.08
subset(whately_2015$temperature, whately_2015$when == '2015-06-21 16:40:00')
## [1] 20.94
subset(whately_2015$temperature, whately_2015$when == '2015-12-22 04:50:00')
## [1] 8.75
p = whatly_2015plot + ggtitle("Average Temperature over Each 10-minute Interal in Whatly, Massachusetts")+
geom_text(x = as.POSIXct('2015-03-20 22:50:00'), y = -5.654,label='Vernal Equinoxe:03-20')+
geom_point(x=as.POSIXct('2015-03-20 22:50:00'), y = -4.654,color='red',size=2)+
geom_text(x= as.POSIXct('2015-10-03 08:20:00'), y = 26.08,label='Autumnal Equinoxe:09-23')+
geom_point(x= as.POSIXct('2015-09-23 08:20:00'), y = 24.08,color='red',size=2)+
geom_text(x = as.POSIXct('2015-06-21 16:40:00'), y = 22.94,label='Summer Solstices:06-21')+
geom_point(x = as.POSIXct('2015-06-21 16:40:00'), y = 20.94,color='red',size=2)+
geom_text(x = as.POSIXct('2015-12-01 04:50:00'), y = 7.75,label='Winter Solstices:12-22')+
geom_point(x = as.POSIXct('2015-12-22 04:50:00'), y = 8.75,color='red',size=2)
p
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
#11.2
head(whately_2015)
text <- bind_rows(data.frame(x = as.POSIXct('2015-11-01 01:00:00'), y = 33, label =paste("12/27, cold front swung "),adj = 0),
data.frame(x = as.POSIXct('2015-11-02,01:10:00'), y = -25,
label = paste("Storm affected 1/06"), adj = 0),
data.frame(x = as.POSIXct('2015-11-2 01:10:00'), y = 30,
label = paste("Storm on 01/31 - 2/02"), adj = 0),
data.frame(x = as.POSIXct('2015-02-20 01:10:00'), y = -23,
label = paste("2/08, Boston experienced Storm"), adj = 0),
data.frame(x = as.POSIXct('2015-04-01 01:10:00'), y = 28,
label = paste("2/14, Another winter storm"), adj = 0),
data.frame(x = as.POSIXct('2015-06-01 00:00:00'), y = -22,
label = paste("Storm occurred on the weekend of 02/20-22"), adj = 0),
data.frame(x = as.POSIXct('2015-03-10 00:00:00'), y = -21,
label = paste("Focused on the South and Southeast on 02/25-26"), adj = 0))
## Warning in bind_rows_(x, .id): Unequal factor levels: coercing to character
## Warning in bind_rows_(x, .id): binding character and factor vector,
## coercing into character vector
## Warning in bind_rows_(x, .id): binding character and factor vector,
## coercing into character vector
## Warning in bind_rows_(x, .id): binding character and factor vector,
## coercing into character vector
## Warning in bind_rows_(x, .id): binding character and factor vector,
## coercing into character vector
## Warning in bind_rows_(x, .id): binding character and factor vector,
## coercing into character vector
## Warning in bind_rows_(x, .id): binding character and factor vector,
## coercing into character vector
## Warning in bind_rows_(x, .id): binding character and factor vector,
## coercing into character vector
segments <- bind_rows(
data.frame(x = c(as.POSIXct('2015-11-01 00:00:00'),as.POSIXct('2015-01-01 00:00:00'), as.POSIXct('2015-01-01 00:00:00'), NA), y = c(33, 33, -3, NA)),
data.frame(x = c(as.POSIXct('2015-11-01,00:00:00'), as.POSIXct('2015-01-07 00:00:00'), as.POSIXct('2015-01-07 00:00:00'), NA), y = c(-25, -25, -22, NA)),
data.frame(x=c(as.POSIXct('2015-11-01 01:10:00'),as.POSIXct('2015-02-02 00:00:00'), as.POSIXct('2015-02-02 00:00:00'), NA), y = c(30, 30, 0, NA)),
data.frame(x=c(as.POSIXct('2015-2-20 01:10:00'),as.POSIXct('2015-02-08 00:00:00'), as.POSIXct('2015-02-08 00:00:00'), NA), y = c(-23, -23, -10, NA)),
data.frame(x=c(as.POSIXct('2015-04-01 01:10:00'),as.POSIXct('2015-02-14 00:00:00'), as.POSIXct('2015-02-14 00:00:00'), NA), y = c(28, 28, -10, NA)),
data.frame(x=c(as.POSIXct('2015-06-01 01:10:00'),as.POSIXct('2015-02-22 00:00:00'), as.POSIXct('2015-02-22 00:00:00'), NA), y = c(-22, -22, -10, NA)),
data.frame(x=c(as.POSIXct('2015-03-10 01:10:00'),as.POSIXct('2015-02-26 00:00:00'), as.POSIXct('2015-02-26 00:00:00'), NA), y = c(-21, -21, -11, NA)))
whatly_2015plot + geom_text(data = text, aes(x = x, y = y, label = label),hjust = "left", size = 3) +
geom_path(data = segments, aes(x = x, y = y), col = "blue")+
ggtitle("Average Temperature over Each 10-minute Interal in Whatly, Massachusetts")
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
## Warning: Removed 1 rows containing missing values (geom_path).
#11.3
a <- list(x = as.POSIXct('2015-03-20 22:50:00'),y = -4.654,text = 'Vernal Equinoxe:03-20',color ='red',xref = "x",yref = "y",showarrow = TRUE,arrowhead = 7,ax = 100,ay = 20)
b <- list(x = as.POSIXct('2015-09-23 08:20:00'),y = 24.08,text = 'Autumnal Equinoxe:09-23',xref = "x",yref = "y",showarrow = TRUE,arrowhead = 7,ax = 80,ay = -40)
c <- list(x = as.POSIXct('2015-06-21 16:40:00'),y = 20.94,text = 'Summer Solstices:06-21',xref = "x",yref = "y",showarrow = TRUE,arrowhead = 7,ax = 20,ay = 100)
d<-list(x = as.POSIXct('2015-12-22 04:50:00'),y = 8.75,text = 'Winter Solstices:12-22',xref = "x",yref = "y",showarrow = TRUE,arrowhead = 7,ax = 20,ay = -80)
plot_ly(whately_2015,x=~whately_2015$when, y=~whately_2015$temperature ,mode='line') %>% layout(title = "Average Temperature over Each 10-minute Interal in Whatly, Massachusetts", xaxis = list(title = "When",showgrid = F), yaxis = list(title = "Temperature (degrees Fahrenheit)")) %>%layout(annotations = a)%>% layout(annotations = b)%>% layout(annotations = c)%>% layout(annotations = d)
## No trace type specified:
## Based on info supplied, a 'scatter' trace seems appropriate.
## Read more about this trace type -> https://plot.ly/r/reference/#scatter
#11.4
library(dygraphs)
## Warning: package 'dygraphs' was built under R version 3.6.2
library(xts)
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Registered S3 method overwritten by 'xts':
## method from
## as.zoo.xts zoo
##
## Attaching package: 'xts'
## The following objects are masked from 'package:dplyr':
##
## first, last
library(macleish)
E= xts(x=whately_2015$temperature, order.by = whately_2015$when)
dygraph(data=E,main="Average Temperature over Each 10-minute Interal in Whatly, Massachusetts",xlab = 'when',ylab='Temperature (degrees Fahrenheit)')%>% dyRangeSelector(dateWindow = c(as.POSIXct('2015-01-01 00:00:00'), as.POSIXct('2015-12-31 23:50:00')))
#11.5
library(ggvis)
## Warning: package 'ggvis' was built under R version 3.6.2
##
## Attaching package: 'ggvis'
## The following objects are masked from 'package:plotly':
##
## add_data, hide_legend
## The following object is masked from 'package:ggplot2':
##
## resolution
whately_2015 %>% ggvis(x=~when)%>% layer_lines(y=~temperature) %>% add_axis( "x", title = "When") %>%add_axis("y", title = "Temperature (degrees Fahrenheit)")