R/GeomTimeline.R

#' Create a ggproto object GeomTimeline
#'
#' This function creates a new ggproto object for geom_timeline
#'
#' @return GeomTimeline returns a ggproto object.
#'
#' @importFrom dplyr filter
#' @importFrom ggplot2 aes
#' @importFrom ggplot2 draw_key_point
#' @importFrom ggplot2 Geom
#' @importFrom ggplot2 ggproto
#' @importFrom grid circleGrob
#' @importFrom grid gpar
#' @importFrom magrittr "%>%"
#' @importFrom scales alpha
#' @importFrom stats na.omit
#'
#' @details see geom_timeline for details
#'
#' @export
GeomTimeline<-ggplot2::ggproto(`_class` = "GeomTimeline", `_inherit` = ggplot2::Geom, required_aes = c("x", 'xmin', 'xmax'), optional_aes = c('y', 'color', 'size', 'alpha'), default_aes = ggplot2::aes(y = 0.5, pch = 21, colour = "black", size = 0.01, fill = 'grey', alpha = 0.4, stroke = 1), draw_key = ggplot2::draw_key_point, draw_panel = function(Data, panel_params, coord)
{
  # extract data from xmin to xmax
  Data<-Data %>%
    dplyr::filter(x>=xmin, x<=xmax) %>%
    dplyr::mutate(size = size/max(size, na.rm = TRUE)*0.04) %>%
    stats::na.omit()
  # transform the coordinates for plotting
  Data2Plot <- coord$transform(Data, panel_params)
  # create a function to draw circles
  grid::circleGrob(Data2Plot$x, Data2Plot$y, r = Data2Plot$size, gp = grid::gpar(col = scales::alpha(Data2Plot$colour, Data2Plot$alpha), fill = scales::alpha(Data2Plot$colour, Data2Plot$alpha), alpha = Data2Plot$alpha, fontsize = Data2Plot$size, lwd = Data2Plot$stroke))
})
ekawabata/TheCapstoneProject documentation built on June 27, 2020, 7:58 a.m.