Skip to content

Instantly share code, notes, and snippets.

@rich-iannone
Last active February 28, 2016 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rich-iannone/18bc60ff6350f0793cee to your computer and use it in GitHub Desktop.
Save rich-iannone/18bc60ff6350f0793cee to your computer and use it in GitHub Desktop.
Using several R packages (stationaRy, DiagrammeR, DiagrammeRsvg, magrittr, and dplyr), you can create a time-series plot of weather-related things. Here, I plotted daily rainfall during 2015.
# Load in several packages
devtools::install_github("rich-iannone/DiagrammeR")
library(DiagrammeR)
library(DiagrammeRsvg)
library(magrittr)
library(stationaRy)
library(dplyr)
# Get the daily rainfall values (in mm) during 2015 at
# the Vancouver Internation Airport (YVR)
yvr_rainfall_2015 <-
get_isd_stations(startyear = 1970, endyear = 2015,
lower_lat = 49, upper_lat = 58,
lower_lon = -125, upper_lon = -120) %>%
select_isd_station(name = "vancouver intl") %>%
get_isd_station_data(startyear = 2015,
endyear = 2015,
select_additional_data = "AA1") %>%
select(year, month, day, hour,
period = aa1_1, rainfall_depth = aa1_2) %>%
filter(period == 6) %>%
filter(rainfall_depth != 999.9) %>%
filter(year == 2015) %>%
group_by(year, month, day) %>%
summarise(daily_rainfall = mean(rainfall_depth,
na.rm = TRUE)) %>%
filter(daily_rainfall != 0) %>%
mutate(date = paste0(year, "-",
sprintf("%02d", month), "-",
sprintf("%02d", day))) %>%
bind_rows(
data_frame(year = 2015, month = 1, day = 1,
daily_rainfall = 0, date = "2015-01-01"),
.
)
# Create a series of x,y points for time series plotting
rainfall_pts <-
create_xy_pts(
series_label = "Daily Rainfall",
x = yvr_rainfall_2015$date,
y = yvr_rainfall_2015$daily_rainfall,
line_width = 1.0,
width = 0.05,
height = 0.05,
shape = "circle",
line_color = "blue")
# Create an xy_graph
xy_graph <-
create_xy_graph(
rainfall_pts,
x_name = "Date",
y_name = "Daily Rainfall (mm)",
heading = "#Daily Rainfall (YVR — 2015)",
footer = c("#####Gist at: https://gist.github.com/rich-iannone/18bc60ff6350f0793cee",
"#####DiagrammeR, stationaRy, dplyr, and magrittr were used."),
xy_value_labels = c("date", "numeric"),
include_xy_minima = c(TRUE, FALSE),
bg_color = "white")
# Get vector of node ID values where precipitation values
# are in the top half of the y-scale
high_precip <-
xy_graph %>% select_nodes("graph_component", "xy_pts") %>%
select_nodes("y", ">5.0", "intersect") %>%
get_selection %>% .$nodes
# Create a callout node stating some obvious stuff
high_precip_callout <-
create_nodes(1, label = "High \nPrecipitation",
shape = "plaintext",
fontcolor = "grey50",
x = 5, y = 9.5)
# Create the edges between the high precip points
# and that callout
high_precip_callout_edges <-
create_edges(from = high_precip,
to = rep("1", length(high_precip)),
arrowhead = "none",
headport = "s",
color = "grey85")
# Create a small graph with the callout and the
# points with higher-than-usual precipitation
callout_graph <-
create_graph(nodes_df = high_precip_callout,
edges_df = high_precip_callout_edges)
# Combine these graphs into one
xy_graph <-
combine_graphs(xy_graph, callouts)
# View the graph in the RStudio Viewer
xy_graph %>% render_graph
# Save the graph as a PDF
xy_graph %>% export_graph("yvr_daily_rainfall_2015.pdf")
# Save the graph as a PNG
xy_graph %>% export_graph("yvr_daily_rainfall_2015.png")
@rich-iannone
Copy link
Author

Here is the plot:

yvr_daily_rainfall_2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment