Skip to content

Commit

Permalink
Add non_missing_aes field to Geom.
Browse files Browse the repository at this point in the history
Call remove_missing systematically. #1304
  • Loading branch information
hadley committed Aug 29, 2015
1 parent d69d740 commit 6b1f695
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 60 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Expand Up @@ -97,6 +97,7 @@ Collate:
'geom-contour.r'
'geom-count.r'
'geom-crossbar.r'
'geom-segment.r'
'geom-curve.r'
'geom-defaults.r'
'geom-ribbon.r'
Expand All @@ -117,7 +118,6 @@ Collate:
'geom-pointrange.r'
'geom-quantile.r'
'geom-rug.r'
'geom-segment.r'
'geom-smooth.r'
'geom-spoke.r'
'geom-step.r'
Expand Down
8 changes: 7 additions & 1 deletion R/geom-.r
Expand Up @@ -53,14 +53,20 @@ NULL
#' @export
Geom <- ggproto("Geom",
required_aes = character(),
non_missing_aes = character(),

default_aes = aes(),

draw_key = draw_key_point,

draw_layer = function(self, data, params, panel, coord) {
args <- c(list(quote(data), quote(panel_scales), quote(coord)), params)
data <- remove_missing(data, isTRUE(params$na.rm),
c(self$required_aes, self$non_missing_aes),
snake_class(self)
)
if (empty(data)) return(list(zeroGrob()))

args <- c(list(quote(data), quote(panel_scales), quote(coord)), params)
plyr::dlply(data, "PANEL", function(data) {
if (empty(data)) return(zeroGrob())

Expand Down
18 changes: 3 additions & 15 deletions R/geom-curve.r
Expand Up @@ -25,19 +25,13 @@ geom_curve <- function(mapping = NULL, data = NULL, stat = "identity",
}

#' @rdname ggplot2-ggproto
#' @include geom-segment.r
#' @format NULL
#' @usage NULL
#' @export
GeomCurve <- ggproto("GeomCurve", Geom,
GeomCurve <- ggproto("GeomCurve", GeomSegment,
draw_panel = function(data, panel_scales, coord, curvature = 0.5, angle = 90,
ncp = 5, arrow = NULL, lineend = "butt", na.rm = FALSE) {

data <- remove_missing(data, na.rm = na.rm,
c("x", "y", "xend", "yend", "linetype", "size", "shape"),
name = "geom_curve")

if (empty(data)) return(zeroGrob())

if (!coord$is_linear()) {
warning("geom_curve is not implemented for non-linear coordinates",
call. = FALSE)
Expand All @@ -55,11 +49,5 @@ GeomCurve <- ggproto("GeomCurve", Geom,
lineend = trans$lineend),
arrow = arrow
)
},

required_aes = c("x", "y", "xend", "yend"),

default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA),

draw_key = draw_key_path
}
)
16 changes: 6 additions & 10 deletions R/geom-dotplot.r
Expand Up @@ -163,6 +163,11 @@ geom_dotplot <- function(mapping = NULL, data = NULL,
#' @usage NULL
#' @export
GeomDotplot <- ggproto("GeomDotplot", Geom,
required_aes = c("x", "y"),
non_missing_aes = c("size", "shape"),

default_aes = aes(colour = "black", fill = "black", alpha = NA),

setup_data = function(data, params) {
data$width <- data$width %||%
params$width %||% (resolution(data$x, FALSE) * 0.9)
Expand Down Expand Up @@ -238,10 +243,6 @@ GeomDotplot <- ggproto("GeomDotplot", Geom,
draw_group = function(data, panel_scales, coord, na.rm = FALSE,
binaxis = "x", stackdir = "up", stackratio = 1,
dotsize = 1, stackgroups = FALSE) {

data <- remove_missing(data, na.rm, c("x", "y", "size", "shape"), name = "geom_dotplot")
if (empty(data)) return(zeroGrob())

if (!coord$is_linear()) {
warning("geom_dotplot does not work properly with non-linear coordinates.")
}
Expand Down Expand Up @@ -270,10 +271,5 @@ GeomDotplot <- ggproto("GeomDotplot", Geom,
)
},

draw_key = draw_key_dotplot,

required_aes = c("x", "y"),

default_aes = aes(colour = "black", fill = "black", alpha = NA)

draw_key = draw_key_dotplot
)
14 changes: 8 additions & 6 deletions R/geom-label.R
Expand Up @@ -38,11 +38,18 @@ geom_label <- function(mapping = NULL, data = NULL, stat = "identity",
#' @usage NULL
#' @export
GeomLabel <- ggproto("GeomLabel", Geom,
required_aes = c("x", "y", "label"),

default_aes = aes(
colour = "black", fill = "white", size = 3.88, angle = 0,
hjust = 0.5, vjust = 0.5, alpha = NA, family = "", fontface = 1,
lineheight = 1.2
),

draw_panel = function(self, data, panel_scales, coord, parse = FALSE,
na.rm = FALSE,
label.padding = unit(0.25, "lines"),
label.r = unit(0.15, "lines")) {
data <- remove_missing(data, na.rm, c("x", "y", "label"), name = "geom_label")
lab <- data$label
if (parse) {
lab <- parse(text = lab)
Expand Down Expand Up @@ -82,11 +89,6 @@ GeomLabel <- ggproto("GeomLabel", Geom,
ggname("geom_label", grobTree(children = grobs))
},

required_aes = c("x", "y", "label"),

default_aes = aes(colour = "black", fill = "white", size = 3.88, angle = 0,
hjust = 0.5, vjust = 0.5, alpha = NA, family = "", fontface = 1, lineheight = 1.2),

draw_key = draw_key_label
)

Expand Down
17 changes: 8 additions & 9 deletions R/geom-point.r
Expand Up @@ -136,11 +136,14 @@ geom_point <- function(mapping = NULL, data = NULL, stat = "identity",
#' @usage NULL
#' @export
GeomPoint <- ggproto("GeomPoint", Geom,
draw_panel = function(data, panel_scales, coord, na.rm = FALSE) {
data <- remove_missing(data, na.rm, c("x", "y", "size", "shape"),
name = "geom_point")
if (empty(data)) return(zeroGrob())
required_aes = c("x", "y"),
non_missing_aes = c("size", "shape"),
default_aes = aes(
shape = 19, colour = "black", size = 1.5, fill = NA,
alpha = NA, stroke = 0.5
),

draw_panel = function(data, panel_scales, coord, na.rm = FALSE) {
coords <- coord$transform(data, panel_scales)
ggname("geom_point",
pointsGrob(
Expand All @@ -157,9 +160,5 @@ GeomPoint <- ggproto("GeomPoint", Geom,
)
},

draw_key = draw_key_point,

required_aes = c("x", "y"),
default_aes = aes(shape = 19, colour = "black", size = 1.5, fill = NA,
alpha = NA, stroke = 0.5)
draw_key = draw_key_point
)
11 changes: 4 additions & 7 deletions R/geom-raster.r
Expand Up @@ -38,6 +38,10 @@ geom_raster <- function(mapping = NULL, data = NULL, stat = "identity",
#' @usage NULL
#' @export
GeomRaster <- ggproto("GeomRaster", Geom,
default_aes = aes(fill = "grey20", alpha = NA),
non_missing_aes = "fill",
required_aes = c("x", "y"),

setup_data = function(data, params) {
hjust <- params$hjust %||% 0.5
vjust <- params$vjust %||% 0.5
Expand All @@ -57,8 +61,6 @@ GeomRaster <- ggproto("GeomRaster", Geom,
if (!inherits(coord, "CoordCartesian")) {
stop("geom_raster only works with Cartesian coordinates", call. = FALSE)
}
data <- remove_missing(data, TRUE, c("x", "y", "fill"),
name = "geom_raster")
data <- coord$transform(data, panel_scales)

# Convert vector of data to raster
Expand All @@ -81,10 +83,5 @@ GeomRaster <- ggproto("GeomRaster", Geom,
default.units = "native", interpolate = interpolate
)
},

default_aes = aes(fill = "grey20", alpha = NA),

required_aes = c("x", "y"),

draw_key = draw_key_rect
)
7 changes: 4 additions & 3 deletions R/geom-segment.r
Expand Up @@ -69,6 +69,10 @@ geom_segment <- function(mapping = NULL, data = NULL, stat = "identity",
#' @usage NULL
#' @export
GeomSegment <- ggproto("GeomSegment", Geom,
required_aes = c("x", "y", "xend", "yend"),
non_missing_aes = c("linetype", "size", "shape"),
default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA),

draw_panel = function(data, panel_scales, coord, arrow = NULL,
lineend = "butt", na.rm = FALSE) {

Expand Down Expand Up @@ -104,8 +108,5 @@ GeomSegment <- ggproto("GeomSegment", Geom,
lineend = lineend)
},

required_aes = c("x", "y", "xend", "yend"),
default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA),

draw_key = draw_key_path
)
15 changes: 7 additions & 8 deletions R/geom-text.r
Expand Up @@ -149,11 +149,15 @@ geom_text <- function(mapping = NULL, data = NULL, stat = "identity",
#' @usage NULL
#' @export
GeomText <- ggproto("GeomText", Geom,
required_aes = c("x", "y", "label"),

default_aes = aes(
colour = "black", size = 3.88, angle = 0, hjust = 0.5,
vjust = 0.5, alpha = NA, family = "", fontface = 1, lineheight = 1.2
),

draw_panel = function(data, panel_scales, coord, parse = FALSE,
na.rm = FALSE, check_overlap = FALSE) {
data <- remove_missing(data, na.rm,
c("x", "y", "label"), name = "geom_text")

lab <- data$label
if (parse) {
lab <- parse(text = lab)
Expand Down Expand Up @@ -183,11 +187,6 @@ GeomText <- ggproto("GeomText", Geom,
)
},

required_aes = c("x", "y", "label"),

default_aes = aes(colour = "black", size = 3.88, angle = 0, hjust = 0.5,
vjust = 0.5, alpha = NA, family = "", fontface = 1, lineheight = 1.2),

draw_key = draw_key_text
)

Expand Down

0 comments on commit 6b1f695

Please sign in to comment.