R/Geom_Timeline.R

Defines functions geom_timeline

Documented in geom_timeline

#' @title Function to visualize of the timeline of the Earthquakes
#'
#' @param data A Dataframe
#' @inheritParams ggplot2::layer
#' @param na.rm specifies how default missing values are approached
#' @param ... additional parameters
#'
#' @return A Plot of timeline of Earthquakes
#'
#' @import ggplot2
#'
#' @examples
#' \dontrun{
#' data = system.file('extdata', 'earthquakesData.tsv', package ='masteringSoftwareDevelopmentinR') %>%
#' data = read_data(data) %>%
#' eq_clean_data() %>%
#' dplyr::filter(datetime >= "1980-01-01" & datetime <="2014-01-01" & COUNTRY == c("BRAZIL"))%>%
#' ggplot() +
#' geom_timeline(aes(x = datetime, size = EQ_MAG_ML, color = DEATHS, fill = DEATHS))
#' }
#'
#' @export
#'

geom_timeline <- function(mapping = NULL, data = NULL, stat = "identity",
                          position = "identity", na.rm = FALSE, show.legend = NA,
                          inherit.aes = TRUE, ...) {
  ggplot2::layer(
    geom = GeomTimeline, mapping = mapping,  data = data, stat = stat,
    position = position, show.legend = show.legend, inherit.aes = inherit.aes,
    params = list(na.rm = na.rm, ...)
  )
}


GeomTimeline <- ggplot2::ggproto("GeomTimeline", ggplot2::Geom, required_aes = c('x'),
                                 default_aes = ggplot2::aes(y = 0,size = 1,color = 'grey50',
                                   alpha = 0.5,shape = 19,stroke = 0.5,fill = NA),

                                 draw_key = ggplot2::draw_key_point,

                                 draw_panel = function(data, panel_scales, coord) {

                                      coords <- coord$transform(data, panel_scales)

                                   coords$size <-coords$size / max(coords$size) * 1.5

                                   grid::pointsGrob(x = coords$x,y = coords$y,
                                     pch = coords$shape,gp = grid::gpar(col = coords$colour,
                                                                        alpha = coords$alpha,
                                                                        cex = coords$size)
)})
SanjayShetty01/masteringSoftwareDevelopmentinR documentation built on Dec. 18, 2021, 12:57 p.m.