Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename size aesthetic for line-based geoms #3672

Merged
merged 14 commits into from
Jun 15, 2022
Merged
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ Collate:
'scale-hue.r'
'scale-identity.r'
'scale-linetype.r'
'scale-linewidth.R'
'scale-manual.r'
'scale-shape.r'
'scale-size.r'
Expand Down
7 changes: 7 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,13 @@ export(scale_linetype_continuous)
export(scale_linetype_discrete)
export(scale_linetype_identity)
export(scale_linetype_manual)
export(scale_linewidth)
export(scale_linewidth_binned)
export(scale_linewidth_continuous)
export(scale_linewidth_date)
export(scale_linewidth_datetime)
export(scale_linewidth_discrete)
export(scale_linewidth_ordinal)
export(scale_radius)
export(scale_shape)
export(scale_shape_binned)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# ggplot2 (development version)

* A `linewidth` aesthetic has been introduced and supersedes the `size`
aesthetic for scaling the width of lines in line based geoms. `size` will
remain functioning but deprecated for these geoms and it is recommended to
update all code to reflect the new aesthetic (@thomasp85, #3672)

* Secondary axis ticks are now positioned more precisely, removing small visual
artefacts with alignment between grid and ticks (@thomasp85, #3576)

Expand Down
10 changes: 9 additions & 1 deletion R/geom-.r
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ Geom <- ggproto("Geom",

# Combine data with defaults and set aesthetics from parameters
use_defaults = function(self, data, params = list(), modifiers = aes()) {
# Inherit size as linewidth if no linewidth aesthetic and param exist
if (self$rename_size && is.null(data$linewidth) && is.null(params$linewidth)) {
data$linewidth <- data$size
params$linewidth <- params$size
}
# Fill in missing aesthetics with their defaults
missing_aes <- setdiff(names(self$default_aes), names(data))

Expand Down Expand Up @@ -187,7 +192,10 @@ Geom <- ggproto("Geom",
required_aes <- unlist(strsplit(self$required_aes, '|', fixed = TRUE))
}
c(union(required_aes, names(self$default_aes)), self$optional_aes, "group")
}
},

# Should the geom rename size to linewidth?
rename_size = FALSE

)

Expand Down
8 changes: 5 additions & 3 deletions R/geom-abline.r
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ NULL
#' @section Aesthetics:
#' These geoms are drawn using with [geom_line()] so support the
#' same aesthetics: `alpha`, `colour`, `linetype` and
#' `size`. They also each have aesthetics that control the position of
#' `linewidth`. They also each have aesthetics that control the position of
#' the line:
#'
#' - `geom_vline()`: `xintercept`
Expand Down Expand Up @@ -141,8 +141,10 @@ GeomAbline <- ggproto("GeomAbline", Geom,
GeomSegment$draw_panel(unique(data), panel_params, coord, lineend = lineend)
},

default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA),
default_aes = aes(colour = "black", linewidth = 0.5, linetype = 1, alpha = NA),
required_aes = c("slope", "intercept"),

draw_key = draw_key_abline
draw_key = draw_key_abline,

rename_size = TRUE
)
10 changes: 6 additions & 4 deletions R/geom-boxplot.r
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,

common <- list(
colour = data$colour,
size = data$size,
linewidth = data$linewidth,
linetype = data$linetype,
fill = alpha(data$fill, data$alpha),
group = data$group
Expand Down Expand Up @@ -293,8 +293,10 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,

draw_key = draw_key_boxplot,

default_aes = aes(weight = 1, colour = "grey20", fill = "white", size = 0.5,
alpha = NA, shape = 19, linetype = "solid"),
default_aes = aes(weight = 1, colour = "grey20", fill = "white", size = NULL,
alpha = NA, shape = 19, linetype = "solid", linewidth = 0.5),

required_aes = c("x|y", "lower|xlower", "upper|xupper", "middle|xmiddle", "ymin|xmin", "ymax|xmax")
required_aes = c("x|y", "lower|xlower", "upper|xupper", "middle|xmiddle", "ymin|xmin", "ymax|xmax"),

rename_size = TRUE
)
4 changes: 3 additions & 1 deletion R/geom-col.r
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,7 @@ GeomCol <- ggproto("GeomCol", GeomRect,
lineend = lineend,
linejoin = linejoin
)
}
},

rename_size = TRUE
)
2 changes: 1 addition & 1 deletion R/geom-contour.r
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ GeomContour <- ggproto("GeomContour", GeomPath,
default_aes = aes(
weight = 1,
colour = "#3366FF",
size = 0.5,
linewidth = 0.5,
linetype = 1,
alpha = NA
)
Expand Down
8 changes: 4 additions & 4 deletions R/geom-crossbar.r
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ GeomCrossbar <- ggproto("GeomCrossbar", Geom,
GeomErrorbar$setup_data(data, params)
},

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

required_aes = c("x", "y", "ymin|xmin", "ymax|xmax"),
Expand All @@ -52,7 +52,7 @@ GeomCrossbar <- ggproto("GeomCrossbar", Geom,
flipped_aes = FALSE) {
data <- flip_data(data, flipped_aes)

middle <- transform(data, x = xmin, xend = xmax, yend = y, size = size * fatten, alpha = NA)
middle <- transform(data, x = xmin, xend = xmax, yend = y, linewidth = linewidth * fatten, alpha = NA)

has_notch <- !is.null(data$ynotchlower) && !is.null(data$ynotchupper) &&
!is.na(data$ynotchlower) && !is.na(data$ynotchupper)
Expand Down Expand Up @@ -82,7 +82,7 @@ GeomCrossbar <- ggproto("GeomCrossbar", Geom,
),
alpha = rep(data$alpha, 11),
colour = rep(data$colour, 11),
size = rep(data$size, 11),
linewidth = rep(data$linewidth, 11),
linetype = rep(data$linetype, 11),
fill = rep(data$fill, 11),
group = rep(seq_len(nrow(data)), 11)
Expand All @@ -94,7 +94,7 @@ GeomCrossbar <- ggproto("GeomCrossbar", Geom,
y = c(data$ymax, data$ymin, data$ymin, data$ymax, data$ymax),
alpha = rep(data$alpha, 5),
colour = rep(data$colour, 5),
size = rep(data$size, 5),
linewidth = rep(data$linewidth, 5),
linetype = rep(data$linetype, 5),
fill = rep(data$fill, 5),
group = rep(seq_len(nrow(data)), 5) # each bar forms it's own group
Expand Down
4 changes: 2 additions & 2 deletions R/geom-curve.r
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ geom_curve <- function(mapping = NULL, data = NULL,
#' @usage NULL
#' @export
GeomCurve <- ggproto("GeomCurve", GeomSegment,
default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA),
default_aes = aes(colour = "black", linewidth = 0.5, linetype = 1, alpha = NA),
draw_panel = function(data, panel_params, coord, curvature = 0.5, angle = 90,
ncp = 5, arrow = NULL, arrow.fill = NULL, lineend = "butt", na.rm = FALSE) {

Expand All @@ -60,7 +60,7 @@ GeomCurve <- ggproto("GeomCurve", GeomSegment,
gp = gpar(
col = alpha(trans$colour, trans$alpha),
fill = alpha(arrow.fill, trans$alpha),
lwd = trans$size * .pt,
lwd = trans$linewidth * .pt,
lty = trans$linetype,
lineend = lineend),
arrow = arrow
Expand Down
2 changes: 1 addition & 1 deletion R/geom-density2d.r
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ geom_density2d <- geom_density_2d
#' @usage NULL
#' @export
GeomDensity2d <- ggproto("GeomDensity2d", GeomPath,
default_aes = aes(colour = "#3366FF", size = 0.5, linetype = 1, alpha = NA)
default_aes = aes(colour = "#3366FF", linewidth = 0.5, linetype = 1, alpha = NA)
)

#' @export
Expand Down
8 changes: 5 additions & 3 deletions R/geom-errorbar.r
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ geom_errorbar <- function(mapping = NULL, data = NULL,
#' @usage NULL
#' @export
GeomErrorbar <- ggproto("GeomErrorbar", Geom,
default_aes = aes(colour = "black", size = 0.5, linetype = 1, width = 0.5,
default_aes = aes(colour = "black", linewidth = 0.5, linetype = 1, width = 0.5,
alpha = NA),

draw_key = draw_key_path,
Expand Down Expand Up @@ -61,12 +61,14 @@ GeomErrorbar <- ggproto("GeomErrorbar", Geom,
y = y,
colour = rep(data$colour, each = 8),
alpha = rep(data$alpha, each = 8),
size = rep(data$size, each = 8),
linewidth = rep(data$linewidth, each = 8),
linetype = rep(data$linetype, each = 8),
group = rep(1:(nrow(data)), each = 8),
row.names = 1:(nrow(data) * 8)
))
data <- flip_data(data, flipped_aes)
GeomPath$draw_panel(data, panel_params, coord, lineend = lineend)
}
},

rename_size = TRUE
)
8 changes: 5 additions & 3 deletions R/geom-errorbarh.r
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ geom_errorbarh <- function(mapping = NULL, data = NULL,
#' @usage NULL
#' @export
GeomErrorbarh <- ggproto("GeomErrorbarh", Geom,
default_aes = aes(colour = "black", size = 0.5, linetype = 1, height = 0.5,
default_aes = aes(colour = "black", linewidth = 0.5, linetype = 1, height = 0.5,
alpha = NA),

draw_key = draw_key_path,
Expand All @@ -73,10 +73,12 @@ GeomErrorbarh <- ggproto("GeomErrorbarh", Geom,
y = as.vector(rbind(data$ymin, data$ymax, NA, data$y, data$y, NA, data$ymin, data$ymax)),
colour = rep(data$colour, each = 8),
alpha = rep(data$alpha, each = 8),
size = rep(data$size, each = 8),
linewidth = rep(data$linewidth, each = 8),
linetype = rep(data$linetype, each = 8),
group = rep(1:(nrow(data)), each = 8),
row.names = 1:(nrow(data) * 8)
)), panel_params, coord, lineend = lineend)
}
},

rename_size = TRUE
)
8 changes: 5 additions & 3 deletions R/geom-hex.r
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ GeomHex <- ggproto("GeomHex", Geom,
gp = gpar(
col = coords$colour,
fill = alpha(coords$fill, coords$alpha),
lwd = coords$size * .pt,
lwd = coords$linewidth * .pt,
lty = coords$linetype,
lineend = lineend,
linejoin = linejoin,
Expand All @@ -106,12 +106,14 @@ GeomHex <- ggproto("GeomHex", Geom,
default_aes = aes(
colour = NA,
fill = "grey50",
size = 0.5,
linewidth = 0.5,
linetype = 1,
alpha = NA
),

draw_key = draw_key_polygon
draw_key = draw_key_polygon,

rename_size = TRUE
)


Expand Down
6 changes: 4 additions & 2 deletions R/geom-hline.r
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ GeomHline <- ggproto("GeomHline", Geom,
GeomSegment$draw_panel(unique(data), panel_params, coord, lineend = lineend)
},

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

draw_key = draw_key_path
draw_key = draw_key_path,

rename_size = TRUE
)
6 changes: 4 additions & 2 deletions R/geom-linerange.r
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ geom_linerange <- function(mapping = NULL, data = NULL,
#' @usage NULL
#' @export
GeomLinerange <- ggproto("GeomLinerange", Geom,
default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA),
default_aes = aes(colour = "black", linewidth = 0.5, linetype = 1, alpha = NA),

draw_key = draw_key_linerange,

Expand All @@ -118,5 +118,7 @@ GeomLinerange <- ggproto("GeomLinerange", Geom,
data <- transform(data, xend = x, y = ymin, yend = ymax)
data <- flip_data(data, flipped_aes)
ggname("geom_linerange", GeomSegment$draw_panel(data, panel_params, coord, lineend = lineend))
}
},

rename_size = TRUE
)
2 changes: 1 addition & 1 deletion R/geom-map.r
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ GeomMap <- ggproto("GeomMap", GeomPolygon,
gp = gpar(
col = data$colour,
fill = alpha(data$fill, data$alpha),
lwd = data$size * .pt,
lwd = data$linewidth * .pt,
lineend = lineend,
linejoin = linejoin,
linemitre = linemitre
Expand Down
14 changes: 8 additions & 6 deletions R/geom-path.r
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ geom_path <- function(mapping = NULL, data = NULL,
GeomPath <- ggproto("GeomPath", Geom,
required_aes = c("x", "y"),

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

handle_na = function(self, data, params) {
# Drop missing values at the start or end of a line - can't drop in the
# middle since you expect those to be shown by a break in the line
complete <- stats::complete.cases(data[c("x", "y", "size", "colour", "linetype")])
complete <- stats::complete.cases(data[c("x", "y", "linewidth", "colour", "linetype")])
kept <- stats::ave(complete, data$group, FUN = keep_mid_true)
data <- data[kept, ]

Expand Down Expand Up @@ -170,7 +170,7 @@ GeomPath <- ggproto("GeomPath", Geom,
linetype <- unique(df$linetype)
new_data_frame(list(
solid = identical(linetype, 1) || identical(linetype, "solid"),
constant = nrow(unique(df[, c("alpha", "colour","size", "linetype")])) == 1
constant = nrow(unique(df[, c("alpha", "colour","linewidth", "linetype")])) == 1
), n = 1)
})
solid_lines <- all(attr$solid)
Expand All @@ -192,7 +192,7 @@ GeomPath <- ggproto("GeomPath", Geom,
gp = gpar(
col = alpha(munched$colour, munched$alpha)[!end],
fill = alpha(munched$colour, munched$alpha)[!end],
lwd = munched$size[!end] * .pt,
lwd = munched$linewidth[!end] * .pt,
lty = munched$linetype[!end],
lineend = lineend,
linejoin = linejoin,
Expand All @@ -207,7 +207,7 @@ GeomPath <- ggproto("GeomPath", Geom,
gp = gpar(
col = alpha(munched$colour, munched$alpha)[start],
fill = alpha(munched$colour, munched$alpha)[start],
lwd = munched$size[start] * .pt,
lwd = munched$linewidth[start] * .pt,
lty = munched$linetype[start],
lineend = lineend,
linejoin = linejoin,
Expand All @@ -217,7 +217,9 @@ GeomPath <- ggproto("GeomPath", Geom,
}
},

draw_key = draw_key_path
draw_key = draw_key_path,

rename_size = TRUE
)

# Trim false values from left and right: keep all values from
Expand Down
4 changes: 2 additions & 2 deletions R/geom-pointrange.r
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ geom_pointrange <- function(mapping = NULL, data = NULL,
#' @usage NULL
#' @export
GeomPointrange <- ggproto("GeomPointrange", Geom,
default_aes = aes(colour = "black", size = 0.5, linetype = 1, shape = 19,
fill = NA, alpha = NA, stroke = 1),
default_aes = aes(colour = "black", size = 0.5, linewidth = 0.5, linetype = 1,
shape = 19, fill = NA, alpha = NA, stroke = 1),

draw_key = draw_key_pointrange,

Expand Down
Loading