R/stat_hurricane.R

Defines functions stat_hurricane

StatHurricane <- ggplot2::ggproto(
  "StatHurricane",
  ggplot2::Stat,
  compute_group = function(data, scales, scale_radii) {
    convert = 1852
    ne <-
      geosphere::destPoint(
        p = c(data$x, data$y),
        b = 1:90,
        d = data$r_ne * scale_radii * convert
      )

    se <-
      geosphere::destPoint(
        p = c(data$x, data$y),
        b = 90:180,
        d = data$r_se * scale_radii * convert
      )

    sw <-
      geosphere::destPoint(
        p = c(data$x, data$y),
        b = 180:270,
        d = data$r_sw * scale_radii * convert
      )

    nw <-
      geosphere::destPoint(
        p = c(data$x, data$y),
        b = 270:360,
        d = data$r_nw * scale_radii * convert
      )

    data <-
      data.frame(rbind(ne, nw, sw, se))
    names(data) <- c("x", "y")
    data
  },
  required_aes = c("x", "y", "r_ne", "r_nw", "r_sw", "r_se")
)

stat_hurricane <-
  function(mapping = NULL,
           data = NULL,
           geom = "polygon",
           position = "identity",
           show.legend = NA,
           outliers = TRUE,
           inherit.aes = TRUE,
           scale_radii = 1,
           ...)
  {

  ggplot2::layer(
    stat = StatHurricane,
    data = data,
    mapping = mapping,
    geom = geom,
    position = position,
    show.legend = show.legend,
    inherit.aes = inherit.aes,
    params = list(outliers = outliers, scale_radii = scale_radii, ...)
  )
}
mykoCheng/ANewGeom documentation built on May 5, 2019, 12:31 p.m.