Skip to content

Commit

Permalink
heap -> dots
Browse files Browse the repository at this point in the history
  • Loading branch information
mjskay committed Aug 26, 2019
1 parent 6a55dd1 commit f662760
Show file tree
Hide file tree
Showing 30 changed files with 224 additions and 206 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Collate:
"gather_variables.R"
"geom.R"
"geom_slabinterval.R"
"geom_heapinterval.R"
"geom_dotsinterval.R"
"geom_interval.R"
"geom_intervalh.R"
"geom_lineribbon.R"
Expand All @@ -112,7 +112,7 @@ Collate:
"stat_dist_slabinterval.R"
"stat_sample_slabinterval.R"
"geom_eye.R"
"stat_heapinterval.R"
"stat_dotsinterval.R"
"stat_pointinterval.R"
"stat_pointintervalh.R"
"stat_interval.R"
Expand Down
26 changes: 13 additions & 13 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ S3method(gather_emmeans_draws,emm_list)
S3method(get_variables,default)
S3method(get_variables,mcmc)
S3method(get_variables,mcmc.list)
S3method(makeContent,heap_grob)
S3method(makeContent,dots_grob)
S3method(parse_dist,character)
S3method(parse_dist,data.frame)
S3method(parse_dist,default)
Expand Down Expand Up @@ -78,13 +78,13 @@ export(gather_pairs)
export(gather_samples)
export(gather_terms)
export(gather_variables)
export(geom_dots)
export(geom_dotsh)
export(geom_dotsinterval)
export(geom_dotsintervalh)
export(geom_eye)
export(geom_eyeh)
export(geom_halfeyeh)
export(geom_heap)
export(geom_heaph)
export(geom_heapinterval)
export(geom_heapintervalh)
export(geom_interval)
export(geom_intervalh)
export(geom_lineribbon)
Expand Down Expand Up @@ -181,16 +181,16 @@ export(stat_dist_ccdfinterval)
export(stat_dist_ccdfintervalh)
export(stat_dist_cdfinterval)
export(stat_dist_cdfintervalh)
export(stat_dist_dots)
export(stat_dist_dotsh)
export(stat_dist_dotsinterval)
export(stat_dist_dotsintervalh)
export(stat_dist_eye)
export(stat_dist_eyeh)
export(stat_dist_gradientinterval)
export(stat_dist_gradientintervalh)
export(stat_dist_halfeye)
export(stat_dist_halfeyeh)
export(stat_dist_heap)
export(stat_dist_heaph)
export(stat_dist_heapinterval)
export(stat_dist_heapintervalh)
export(stat_dist_interval)
export(stat_dist_intervalh)
export(stat_dist_lineribbon)
Expand All @@ -199,16 +199,16 @@ export(stat_dist_pointintervalh)
export(stat_dist_slab)
export(stat_dist_slabh)
export(stat_dist_slabinterval)
export(stat_dots)
export(stat_dotsh)
export(stat_dotsinterval)
export(stat_dotsintervalh)
export(stat_eye)
export(stat_eyeh)
export(stat_gradientinterval)
export(stat_gradientintervalh)
export(stat_halfeye)
export(stat_halfeyeh)
export(stat_heap)
export(stat_heaph)
export(stat_heapinterval)
export(stat_heapintervalh)
export(stat_histinterval)
export(stat_histintervalh)
export(stat_interval)
Expand Down
2 changes: 1 addition & 1 deletion R/binning_methods.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# binning methods for use with heap geom
# binning methods for use with dots geom
#
# Author: mjskay
###############################################################################
Expand Down
89 changes: 45 additions & 44 deletions R/geom_heapinterval.R → R/geom_dotsinterval.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@



# heap_grob ---------------------------------------------------------------
# dots_grob ---------------------------------------------------------------

#' @importFrom ggplot2 .stroke .pt
heap_grob = function(data, max_height, x, y,
dots_grob = function(data, max_height, x, y,
name = NULL, gp = gpar(), vp = NULL,
dotsize = 1, stackratio = 1, side = "top"
) {
Expand All @@ -20,14 +20,14 @@ heap_grob = function(data, max_height, x, y,
gTree(
datas = datas, max_height = max_height, x_ = x, y_ = y,
dotsize = dotsize, stackratio = stackratio, side = side,
name = name, gp = gp, vp = vp, cl = "heap_grob"
name = name, gp = gp, vp = vp, cl = "dots_grob"
)
}


#' @importFrom grDevices nclass.Sturges
#' @export
makeContent.heap_grob = function(x) {
makeContent.dots_grob = function(x) {
grob_ = x
datas = grob_$datas
max_height = grob_$max_height
Expand Down Expand Up @@ -183,7 +183,7 @@ makeContent.heap_grob = function(x) {

# panel drawing function -------------------------------------------------------

draw_slabs_heap = function(self, s_data, panel_params, coord,
draw_slabs_dots = function(self, s_data, panel_params, coord,
side, scale, orientation, justification, normalize,
child_params
) {
Expand All @@ -196,7 +196,7 @@ draw_slabs_heap = function(self, s_data, panel_params, coord,
))

if (!coord$is_linear()) {
stop("geom_heapinterval does not work properly with non-linear coordinates.")
stop("geom_dotsinterval does not work properly with non-linear coordinates.")
}
# Swap axes if using coord_flip
if (inherits(coord, "CoordFlip")) {
Expand All @@ -205,9 +205,9 @@ draw_slabs_heap = function(self, s_data, panel_params, coord,
}
s_data = coord$transform(s_data, panel_params)

# draw the heap grob (which will draw dotplots for all the slabs)
# draw the dots grob (which will draw dotplots for all the slabs)
max_height = max(s_data[[ymax]] - s_data[[ymin]])
slab_grobs = list(heap_grob(s_data, max_height, x, y,
slab_grobs = list(dots_grob(s_data, max_height, x, y,
dotsize = child_params$dotsize,
stackratio = child_params$stackratio,
side = side
Expand All @@ -223,35 +223,36 @@ draw_slabs_heap = function(self, s_data, panel_params, coord,
}


# geom_heapinterval ---------------------------------------------------------------
# geom_dotsinterval ---------------------------------------------------------------

#' Automatic dotplots ("heaps"), heap + intervals, and quantile dotplots (ggplot geom)
#' Automatic dotplots, dots + intervals, and quantile dotplots (ggplot geom)
#'
#' Geoms and stats for creating dotplots ("heaps") that automatically determines a bin width that
#' Geoms and stats for creating dotplots that automatically determines a bin width that
#' ensures the plot fits within the available space. Also ensures dots do not overlap, and allows
#' generation of quantile dotplots using the \code{quantiles} argument to \code{stat_heapinterval}/\code{stat_heap}
#' and \code{stat_dist_heapinterval}/\code{stat_dist_heap}. Generally follows the naming scheme and
#' generation of quantile dotplots using the \code{quantiles} argument to \code{stat_dotsinterval}/\code{stat_dots}
#' and \code{stat_dist_dotsinterval}/\code{stat_dist_dots}. Generally follows the naming scheme and
#' arguments of the \code{\link{geom_slabinterval}} and \code{\link{stat_slabinterval}} family of
#' geoms and stats.
#'
#' The "heap" geoms are similar to \code{\link{geom_dotplot}} but with a number of differences:
#' The dots geoms are similar to \code{\link{geom_dotplot}} but with a number of differences:
#'
#' \itemize{
#' \item Heaps act like slabs in \code{\link{geom_slabinterval}} and can be given x positions (or y positions when
#' \item Dots geoms act like slabs in \code{\link{geom_slabinterval}} and can be given x positions (or y positions when
#' in a horizontal orientation).
#' \item Given the available space to lay out dots, heaps will automatically determine how many bins to use to fit
#' the available space.
#' \item Heaps use a center-out layout algorithm that guarantees that symmetrical data results in a symmetrical
#' plot and avoids overlapping dots.
#' \item The shape of the dots in a heap can be changed using the \code{slab_shape} aesthetic (when using the
#' \code{heapinterval} family) or the \code{shape} or \code{slab_shape} aesthetic (when using the \code{heap} family)
#' \item Given the available space to lay out dots, the dots geoms will automatically determine how many bins to
#' use to fit the available space.
#' \item Dots geoms use a dynamic layout algorithm that lays out dots from the center out if the input data are
#' symmetrical, guaranteeing that symmetrical data results in a symmetrical plot. The layout algorithm also prevents
#' dots from overlapping each other.
#' \item The shape of the dots in a in these geoms can be changed using the \code{slab_shape} aesthetic (when using the
#' \code{dotsinterval} family) or the \code{shape} or \code{slab_shape} aesthetic (when using the \code{dots} family)
#' }
#'
#' The \code{stat_...} and \code{stat_dist_...} versions of the stats when used with the \code{quantiles} argument
#' are particularly useful for constructing quantile dotplots, which can be an effective way to communicate uncertainty
#' using a frequency framing that may be easier for laypeople to understand (Kay et al. 2016, Fernandes et al. 2018).
#'
#' @eval rd_slabinterval_aesthetics(geom = GeomHeapinterval, geom_name = "geom_heapinterval", stat = StatHeapinterval)
#' @eval rd_slabinterval_aesthetics(geom = GeomDotsinterval, geom_name = "geom_dotsinterval", stat = StatDotsinterval)
#' @inheritParams geom_slabinterval
#' @inheritParams stat_slabinterval
#' @param ... Other arguments passed to \code{\link{layer}}.
Expand Down Expand Up @@ -283,7 +284,7 @@ draw_slabs_heap = function(self, s_data, panel_params, coord,
#' @importFrom rlang %||%
#' @import grid
#' @export
geom_heapinterval = function(
geom_dotsinterval = function(
mapping = NULL,
data = NULL,
stat = "identity",
Expand All @@ -303,7 +304,7 @@ geom_heapinterval = function(
data = data,
stat = stat,
position = position,
geom = GeomHeapinterval,
geom = GeomDotsinterval,
show.legend = show.legend,
inherit.aes = inherit.aes,

Expand All @@ -315,7 +316,7 @@ geom_heapinterval = function(
)
)
}
GeomHeapinterval = ggproto("GeomHeapinterval", GeomSlabinterval,
GeomDotsinterval = ggproto("GeomDotsinterval", GeomSlabinterval,
default_aes = defaults(aes(
slab_shape = NULL
), GeomSlabinterval$default_aes),
Expand Down Expand Up @@ -393,7 +394,7 @@ GeomHeapinterval = ggproto("GeomHeapinterval", GeomSlabinterval,
data
},

draw_slabs = draw_slabs_heap,
draw_slabs = draw_slabs_dots,

draw_key_slab = function(self, data, key_data, params, size) {
# slab key is different from usual - it's actually a point!
Expand All @@ -419,14 +420,14 @@ GeomHeapinterval = ggproto("GeomHeapinterval", GeomSlabinterval,

# shortcut geoms ----------------------------------------------------------
#' @export
#' @rdname geom_heapinterval
geom_heapintervalh = function(..., orientation = "horizontal") {
geom_heapinterval(..., orientation = orientation)
#' @rdname geom_dotsinterval
geom_dotsintervalh = function(..., orientation = "horizontal") {
geom_dotsinterval(..., orientation = orientation)
}

#' @export
#' @rdname geom_heapinterval
geom_heap = function(
#' @rdname geom_dotsinterval
geom_dots = function(
mapping = NULL,
data = NULL,
stat = "identity",
Expand All @@ -443,7 +444,7 @@ geom_heap = function(
data = data,
stat = stat,
position = position,
geom = GeomHeap,
geom = GeomDots,
show.legend = show.legend,
inherit.aes = inherit.aes,

Expand All @@ -457,19 +458,19 @@ geom_heap = function(
)
)
}
GeomHeap = ggproto("GeomHeap", GeomHeapinterval,
# override these from GeomSlabinterval instead of GeomHeapinterval
# because we want to directly change the base versions, which in geom_heap
# correspond to shape/size/colour of the dots in the heap but in
# geom_heapinterval do not
GeomDots = ggproto("GeomDots", GeomDotsinterval,
# override these from GeomSlabinterval instead of GeomDotsinterval
# because we want to directly change the base versions, which in geom_dots
# correspond to shape/size/colour of the dots in the geom but in
# geom_dotsinterval do not
default_key_aes = defaults(aes(
shape = 21,
size = 0.75,
colour = "gray65"
), GeomSlabinterval$default_key_aes),

override_slab_aesthetics = function(self, s_data) {
# we define these differently from geom_heapinterval to make this easier to use on its own
# we define these differently from geom_dotsinterval to make this easier to use on its own
s_data$colour = s_data$slab_colour %||% s_data$colour
s_data$fill = s_data$slab_fill %||% s_data$fill
s_data$alpha = s_data$slab_alpha %||% s_data$alpha
Expand All @@ -481,7 +482,7 @@ GeomHeap = ggproto("GeomHeap", GeomHeapinterval,
default_params = defaults(list(
show_point = FALSE,
show_interval = FALSE
), GeomHeapinterval$default_params),
), GeomDotsinterval$default_params),

draw_key_slab = function(self, data, key_data, params, size) {
# can drop all the complicated checks from this key since it's just one geom
Expand All @@ -494,10 +495,10 @@ GeomHeap = ggproto("GeomHeap", GeomHeapinterval,
}
)
# have to unset these here because defaults() does not treat NULLs as unsetting values
GeomHeap$default_key_aes$slab_colour = NULL
GeomHeap$default_key_aes$slab_size = NULL
GeomDots$default_key_aes$slab_colour = NULL
GeomDots$default_key_aes$slab_size = NULL
#' @export
#' @rdname geom_heapinterval
geom_heaph = function(..., orientation = "horizontal") {
geom_heap(..., orientation = orientation)
#' @rdname geom_dotsinterval
geom_dotsh = function(..., orientation = "horizontal") {
geom_dots(..., orientation = orientation)
}
2 changes: 1 addition & 1 deletion R/scales.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#' densities of 0 get mapped to 0 in the output.}
#' \item{\code{scale_slab_size_* }}{Slab outline line width}
#' \item{\code{scale_slab_linetype_* }}{Slab outline line type}
#' \item{\code{scale_slab_shape_* }}{Slab dot shape (for \code{\link{geom_heapinterval}})}
#' \item{\code{scale_slab_shape_* }}{Slab dot shape (for \code{\link{geom_dotsinterval}})}
#' }
#'
#' See the corresponding scale documentation in ggplot for more information; e.g.
Expand Down
Loading

0 comments on commit f662760

Please sign in to comment.