#Andrew Spewak
#Mastering Software Development in R Capstone
#' Create 'GeomTimeline' class
#'
#' We use ggproto function to create a new geom class 'Geom_timeline'
#'
#' @param required_aes required aesthetic arguments for the geom_hurricane supplied in character vector
#' @param default_aes default values for aesthetic arguments
#' @param draw_key the function to draw the legend with the associated geom
#' @param draw_panel where the bulk of this geom is constructed
#'
#' @importFrom ggplot2 ggproto
#' @importFrom grid polygonGrob gpar
#'
#' @return a geom class that can be used for plotting a timeline of earthquakes
#'
#' @examples \dontrun{GeomTimeline}
#'
#' @export
GeomTimeline <- ggplot2::ggproto(
"GeomTimeline",
ggplot2::Geom,
#define required inputs
required_aes = c("x"),
#set defaults for optional inputs
default_aes = ggplot2::aes(y = 0.5, shape = 3,size = 1,alpha = 1,fill = NA),
#set the function to draw the legend
draw_key = ggplot2::draw_key_point,
#draw the geom
draw_panel = function(data, panel_scales, coord) {
#transform the data first
coords <- coord$transform(data, panel_scales)
#draw the timeline
Timeline_line_grobs <- grid::polylineGrob(x = grid::unit(rep(c(0, 1),
length(coords$y)),"npc"),
y = rep(coords$y, each = 2),
id.length = rep(2,length(coords$y)),
gp = grid::gpar(col = "black", lwd = 0.3, lty = 1))
#draw the points
Earthquakes_points_grobs <- grid::pointsGrob(
x = coords$x,
y = coords$y,
pch = coords$shape,
gp = grid::gpar(col = alpha(coords$colour, coords$alpha), fill = alpha(coords$fill, coords$alpha),lwd = coords$stroke * .stroke / 2),
fontsize = coords$size * .pt + coords$stroke * .stroke / 2)
#plot the timeline and points
grid::gTree(children = grid::gList(Timeline_line, Earthquakes_points_grobs))})
#' geom_timeline function
#' With the created geom class, we create the actual function that will build a layer based on the geom specification.
#'
#' @param mapping
#' @param data
#' @param stat
#' @param position
#' @param na.rm
#' @param show.legend
#' @param inherit.aes
#'
#' @importFrom ggplot2 layer
#'
#' @examples \dontrun{ggplot() + geom_timeline(aes(x = DATE, y = COUNTRY, color = TOTAL_DEATHS))}
#'
#' @return a function that will build a layer based on geom_timeline geom.
#'
#' @export
geom_timeline <- function(mapping = NULL,
data = NULL,
na.rm = TRUE,
position = "identity",
stat = "identity",
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, ...))
}
#' geom_timeline_label function
#' this is the function that builds the label layer on the geom_timeline geom
#'
#' @param mapping
#' @param data
#' @param stat
#' @param position
#' @param na.rm
#' @param show.legend
#' @param inherit.aes
#'
#' @importFrom ggplot2 layer
#'
#' @examples \dontrun{ggplot() + geom_timeline(aes(x = DATE, y = COUNTRY, color = TOTAL_DEATHS) +
#' geom_timeline_label(aes(x = DATE, y = COUNTRY, magnitude = EQ_PRIMARY, label = LOCATION_NAME, n_max = 5))}
#'
#' @return a function that will build a layer of labels based on geom_timeline geom.
#'
#' @export
geom_timeline_label <- function(mapping = NULL,
data = NULL,
na.rm = TRUE,
show.legend = NA,
stat = "identity",
position = "identity",
inherit.aes = TRUE, ...) {
ggplot2::layer(
geom = GeomTimeLineAnnotation,
mapping = mapping,
data = data,
stat = stat,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(na.rm = na.rm, ...)
)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.