R/plot.R

Defines functions error_prop plot.rvad_vad

#' @export
plot.rvad_vad <- function(x, y, ...) {
  if(!requireNamespace("ggplot2", quietly = TRUE)) {
    stop('ggplot2 package needed. You can install it with `install.packages("ggplot2")')
  }

  if (isTRUE(attr(x, "rvad_raw"))) {
    x <- x[stats::complete.cases(x), ]
    x$elevation <- factor(x$elevation)
    ggplot2::ggplot(x, ggplot2::aes(sqrt(u^2 + v^2), height)) +
      ggplot2::geom_point(ggplot2::aes(color = elevation)) +
      ggplot2::scale_color_viridis_d()
  } else {
    x <- x[stats::complete.cases(x), ]
    x$V <- sqrt(x$u^2 + x$v^2)
    x$dV <- error_prop(x$u, x$v, x$u_std.error, x$v_std.error)

    ggplot2::ggplot(x, ggplot2::aes(height, sqrt(u^2 + v^2))) +
      ggplot2::geom_point() +
      ggplot2::geom_line() +
      ggplot2::geom_ribbon(ggplot2::aes(ymin = V - 2*dV, ymax = V + 2*dV),
                           alpha = 0.2) +
      ggplot2::coord_flip()
  }
}

error_prop <- function(u, v, du, dv) {
  1/sqrt(u^2 + v^2)*sqrt((u*du)^2 + (v*dv)^2)
}
paocorrales/rvad documentation built on July 22, 2020, 3:24 a.m.