R/plot.R

Defines functions plot_dly_tavg plot_dly_prcp_001 plot_dly_snow_001 plot_mly_tavg plot_mly_prcp plot_mly_snow plot_dly plot_mly

Documented in plot_dly_prcp_001 plot_dly_snow_001 plot_dly_tavg plot_mly_prcp plot_mly_snow plot_mly_tavg

# TODO
# plot_dly() and plot_mly() are very similar, combine?

#' Plot daily mean temperature normals by station
#'
#' @param data Data frame of daily normals data
#' @export
plot_dly_tavg <- function(data) {
  plot_dly(data, "tavg")
}

#' Plot daily probability of precipitation normals data by station
#'
#' Probability of precipitation >= 0.01 inches for 29-day windows centered
#' on each day of the year.
#'
#' @param data Data frame of daily normals data
#' @export
plot_dly_prcp_001 <- function(data) {
  plot_dly(data, "prcp_001")
}

#' Plot daily probability of snowfall normals data by station
#'
#' Probability of snowfall >= 0.1 inches for 29-day windows centered
#' on each day of the year.
#'
#' @param data Data frame of daily normals data
#' @export
plot_dly_snow_001 <- function(data) {
  plot_dly(data, "snow_001")
}

#' Plot monthly mean temperature normals by station
#'
#' @param data Data frame of monthly normals data
#' @export
plot_mly_tavg <- function(data) {
  plot_mly(data, "tavg")
}

#' Plot monthly average precipitation normals data by station
#'
#' Long-term averages of monthly precipitation totals.
#'
#' @param data Data frame of monthly normals data
#' @export
plot_mly_prcp <- function(data) {
  plot_mly(data, "prcp")
}

#' Plot monthly average snowfall normals data by station
#'
#' Long-term averages of monthly snowfall totals.
#'
#' @param data Data frame of monthly normals data
#' @export
plot_mly_snow <- function(data) {
  plot_mly(data, "snow")
}

plot_dly <- function(data, y) {
  data <- data[!is.na(data[[y]]), ]
  x <- data$date
  y <- data[[y]]
  color <- substr(data$name, 1, 25)

  n_colors <- length(unique(data$id))
  colors <- c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3", "#FF7F00")
  colors <- rep(colors, ceiling(n_colors / 5))[seq_len(n_colors)]

  out <- plotly::plot_ly(x = x, y = y, color = color, colors = colors)
  out <- plotly::add_lines(out)
  out <- plotly::layout(out,
    xaxis = list(fixedrange = TRUE, tickformat = "%b %e"),
    yaxis = list(fixedrange = TRUE),
    hovermode = "compare"
  )
  out <- plotly::config(out,
    displayModeBar = FALSE,
    doubleClick = FALSE,
    showAxisDragHandles = FALSE
  )
  out
}

plot_mly <- function(data, y) {
  data <- data[!is.na(data[[y]]), ]
  x <- factor(data$month, 1:12, month.abb)
  y <- data[[y]]
  color <- substr(data$name, 1, 25)

  n_colors <- length(unique(data$id))
  colors <- c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3", "#FF7F00")
  colors <- rep(colors, ceiling(n_colors / 5))[seq_len(n_colors)]

  out <- plotly::plot_ly(x = x, y = y, color = color, colors = colors)
  out <- plotly::add_trace(out, type = "scatter", mode = "markers+lines")
  out <- plotly::layout(out,
    xaxis = list(fixedrange = TRUE),
    yaxis = list(fixedrange = TRUE),
    hovermode = "compare"
  )
  out <- plotly::config(out,
    displayModeBar = FALSE,
    doubleClick = FALSE,
    showAxisDragHandles = FALSE
  )
  out
}
rwright88/normals documentation built on Nov. 16, 2019, 1:50 p.m.