title | subtitle | week | type | reading | tasks | |||||
---|---|---|---|---|---|---|---|---|---|---|
Dynamic HTML graph of Daily Temperatures |
Using DyGraph library. |
12 |
Case Study |
|
|
- Browse the HTML Widgets page for many more examples. Take notes in your readme.md about potential uses in your project.
- Explore the DyGraphs webpage
- Download daily weather data for Buffalo, NY using an API
- Generate a dynamic html visualization of the timeseries.
- Save the graph to your project folder using Export->Save as Webpage
In this session you will explore several ways to generate dynamic and interactive data displays. These include making maps and graphs that you can pan/zoom, select features for more information, and interact with in other ways. The most common output format is HTML, which can easily be embedded in a website (such as your final project!).
library(dplyr)
library(ggplot2)
library(ggmap)
library(htmlwidgets)
library(widgetframe)
If you don't have the packages above, install them in the package manager or by running install.packages("widgetframe")
, etc.
Make a dygraph of recent daily maximum temperature data from Buffalo, NY.
First use the following code to download the daily weather data.
library(tidyverse)
library(rnoaa)
library(xts)
library(dygraphs)
d=meteo_tidy_ghcnd("USW00014733",
date_min = "2016-01-01",
var = c("TMAX"),
keep_flags=T) %>%
mutate(date=as.Date(date),
tmax=as.numeric(tmax)/10) #Divide the tmax data by 10 to convert to degrees.
## using cached file: /Users/adamw/Library/Caches/R/noaa_ghcnd/USW00014733.dly
## date created (size, mb): 2020-09-16 11:00:11 (8.417)
## file min/max dates: 1938-05-01 / 2020-09-30
Remaining steps:
- Convert
d
into anxts
time series object usingxts()
. You will need to specifify which column has the data (d$tmax
) andorder.by=d$date
. See?xts
for help. - Use
dygraph()
to draw the plot - Set the title of the dygraph to be
main="Daily Maximum Temperature in Buffalo, NY"
- Add a
dyRangeSelector()
with adateWindow
ofc("2020-01-01", "2020-10-31")
- Explore other options. You could download another variable (precipitation?) and add it to the plot. Or imagine another way to visualize the data using one of the other interactive libraries.
At a minimum, your final graph should look something like this:
<script type="application/json" data-for="htmlwidget-2a768384232cef0f58d6">{"x":{"url":"CS_12_files/figure-html//widgets/widget_unnamed-chunk-3.html","options":{"xdomain":"*","allowfullscreen":false,"lazyload":false}},"evals":[],"jsHooks":[]}</script>What other visualizations can you make with these data?