diff --git a/NAMESPACE b/NAMESPACE index 67c7a4941a..3dbc9d5ca6 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -422,6 +422,7 @@ export(get_alt_text) export(get_element_tree) export(get_guide_data) export(gg_dep) +export(ggpar) export(ggplot) export(ggplotGrob) export(ggplot_add) diff --git a/NEWS.md b/NEWS.md index cd39c0b8ad..dcd22bdd8d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # ggplot2 (development version) +* New helper function `ggpar()` to translate ggplot2's interpretation of + graphical parameters to {grid}'s interpretation (@teunbrand, #5866). + # ggplot2 3.5.1 This is a small release focusing on fixing regressions from 3.5.0 and diff --git a/R/annotation-logticks.R b/R/annotation-logticks.R index 8f3e8a63c2..1d25332a91 100644 --- a/R/annotation-logticks.R +++ b/R/annotation-logticks.R @@ -175,14 +175,14 @@ GeomLogticks <- ggproto("GeomLogticks", Geom, ticks$x_b <- with(data, segmentsGrob( x0 = unit(xticks$x, "native"), x1 = unit(xticks$x, "native"), y0 = unit(xticks$start, "cm"), y1 = unit(xticks$end, "cm"), - gp = gpar(col = alpha(colour, alpha), lty = linetype, lwd = linewidth * .pt) + gp = ggpar(col = alpha(colour, alpha), lty = linetype, lwd = linewidth) )) } if (grepl("t", sides) && nrow(xticks) > 0) { ticks$x_t <- with(data, segmentsGrob( x0 = unit(xticks$x, "native"), x1 = unit(xticks$x, "native"), y0 = unit(1, "npc") - unit(xticks$start, "cm"), y1 = unit(1, "npc") - unit(xticks$end, "cm"), - gp = gpar(col = alpha(colour, alpha), lty = linetype, lwd = linewidth * .pt) + gp = ggpar(col = alpha(colour, alpha), lty = linetype, lwd = linewidth) )) } } @@ -213,14 +213,14 @@ GeomLogticks <- ggproto("GeomLogticks", Geom, ticks$y_l <- with(data, segmentsGrob( y0 = unit(yticks$y, "native"), y1 = unit(yticks$y, "native"), x0 = unit(yticks$start, "cm"), x1 = unit(yticks$end, "cm"), - gp = gpar(col = alpha(colour, alpha), lty = linetype, lwd = linewidth * .pt) + gp = ggpar(col = alpha(colour, alpha), lty = linetype, lwd = linewidth) )) } if (grepl("r", sides) && nrow(yticks) > 0) { ticks$y_r <- with(data, segmentsGrob( y0 = unit(yticks$y, "native"), y1 = unit(yticks$y, "native"), x0 = unit(1, "npc") - unit(yticks$start, "cm"), x1 = unit(1, "npc") - unit(yticks$end, "cm"), - gp = gpar(col = alpha(colour, alpha), lty = linetype, lwd = linewidth * .pt) + gp = ggpar(col = alpha(colour, alpha), lty = linetype, lwd = linewidth) )) } } diff --git a/R/annotation-map.R b/R/annotation-map.R index 86fd0e0952..2e53b3b9c2 100644 --- a/R/annotation-map.R +++ b/R/annotation-map.R @@ -95,9 +95,9 @@ GeomAnnotationMap <- ggproto("GeomAnnotationMap", GeomMap, polygonGrob(coords$x, coords$y, default.units = "native", id = grob_id, - gp = gpar( + gp = ggpar( col = data$colour, fill = alpha(data$fill, data$alpha), - lwd = data$linewidth * .pt) + lwd = data$linewidth) ) }, diff --git a/R/coord-radial.R b/R/coord-radial.R index 62b3bff136..89c84dd5dc 100644 --- a/R/coord-radial.R +++ b/R/coord-radial.R @@ -326,8 +326,8 @@ CoordRadial <- ggproto("CoordRadial", Coord, y = c(Inf, -Inf, -Inf, Inf) ) background <- coord_munch(self, background, panel_params, is_closed = TRUE) - bg_gp <- gpar( - lwd = len0_null(bg_element$linewidth * .pt), + bg_gp <- ggpar( + lwd = bg_element$linewidth, col = bg_element$colour, fill = bg_element$fill, lty = bg_element$linetype ) diff --git a/R/coord-sf.R b/R/coord-sf.R index 331ca4f1f0..12f7371003 100644 --- a/R/coord-sf.R +++ b/R/coord-sf.R @@ -326,9 +326,9 @@ CoordSf <- ggproto("CoordSf", CoordCartesian, if (inherits(el, "element_blank")) { grobs <- list(element_render(theme, "panel.background")) } else { - line_gp <- gpar( + line_gp <- ggpar( col = el$colour, - lwd = len0_null(el$linewidth * .pt), + lwd = el$linewidth, lty = el$linetype ) grobs <- c( diff --git a/R/geom-curve.R b/R/geom-curve.R index a2597a8d72..7ef935caeb 100644 --- a/R/geom-curve.R +++ b/R/geom-curve.R @@ -57,10 +57,10 @@ GeomCurve <- ggproto("GeomCurve", GeomSegment, default.units = "native", curvature = curvature, angle = angle, ncp = ncp, square = FALSE, squareShape = 1, inflect = FALSE, open = TRUE, - gp = gpar( + gp = ggpar( col = alpha(trans$colour, trans$alpha), fill = alpha(arrow.fill, trans$alpha), - lwd = trans$linewidth * .pt, + lwd = trans$linewidth, lty = trans$linetype, lineend = lineend), arrow = arrow diff --git a/R/geom-dotplot.R b/R/geom-dotplot.R index d79e6a823e..cdbc137fe7 100644 --- a/R/geom-dotplot.R +++ b/R/geom-dotplot.R @@ -293,9 +293,9 @@ GeomDotplot <- ggproto("GeomDotplot", Geom, dotstackGrob(stackaxis = stackaxis, x = tdata$x, y = tdata$y, dotdia = dotdianpc, stackposition = tdata$stackpos, stackdir = stackdir, stackratio = stackratio, default.units = "npc", - gp = gpar(col = alpha(tdata$colour, tdata$alpha), + gp = ggpar(col = alpha(tdata$colour, tdata$alpha), fill = fill_alpha(tdata$fill, tdata$alpha), - lwd = tdata$stroke, lty = tdata$linetype, + lwd = tdata$stroke / .pt, lty = tdata$linetype, lineend = lineend)) ) }, diff --git a/R/geom-hex.R b/R/geom-hex.R index a220e12140..19076138dc 100644 --- a/R/geom-hex.R +++ b/R/geom-hex.R @@ -89,10 +89,10 @@ GeomHex <- ggproto("GeomHex", Geom, ggname("geom_hex", polygonGrob( coords$x, coords$y, - gp = gpar( + gp = ggpar( col = data$colour, fill = fill_alpha(data$fill, data$alpha), - lwd = data$linewidth * .pt, + lwd = data$linewidth, lty = data$linetype, lineend = lineend, linejoin = linejoin, diff --git a/R/geom-label.R b/R/geom-label.R index c292fa1a66..30ee6aa388 100644 --- a/R/geom-label.R +++ b/R/geom-label.R @@ -90,17 +90,17 @@ GeomLabel <- ggproto("GeomLabel", Geom, padding = label.padding, r = label.r, angle = row$angle, - text.gp = gpar( + text.gp = ggpar( col = row$colour, fontsize = row$size * size.unit, fontfamily = row$family, fontface = row$fontface, lineheight = row$lineheight ), - rect.gp = gpar( + rect.gp = ggpar( col = if (isTRUE(all.equal(label.size, 0))) NA else row$colour, fill = fill_alpha(row$fill, row$alpha), - lwd = label.size * .pt + lwd = label.size ) ) }) @@ -115,7 +115,7 @@ GeomLabel <- ggproto("GeomLabel", Geom, labelGrob <- function(label, x = unit(0.5, "npc"), y = unit(0.5, "npc"), just = "center", padding = unit(0.25, "lines"), r = unit(0.1, "snpc"), angle = NULL, default.units = "npc", name = NULL, - text.gp = gpar(), rect.gp = gpar(fill = "white"), vp = NULL) { + text.gp = gpar(), rect.gp = ggpar(fill = "white"), vp = NULL) { if (length(label) != 1) { cli::cli_abort("{.arg label} must be of length 1.") @@ -130,7 +130,7 @@ labelGrob <- function(label, x = unit(0.5, "npc"), y = unit(0.5, "npc"), vp <- viewport( angle = angle, x = x, y = y, width = unit(0, "cm"), height = unit(0, "cm"), - gp = gpar(fontsize = text.gp$fontsize) + gp = ggpar(fontsize = text.gp$fontsize) ) x <- unit(rep(0.5, length(x)), "npc") y <- unit(rep(0.5, length(y)), "npc") diff --git a/R/geom-map.R b/R/geom-map.R index 01024ebeff..8086db6209 100644 --- a/R/geom-map.R +++ b/R/geom-map.R @@ -144,10 +144,10 @@ GeomMap <- ggproto("GeomMap", GeomPolygon, data <- data[data_rows, , drop = FALSE] polygonGrob(coords$x, coords$y, default.units = "native", id = grob_id, - gp = gpar( + gp = ggpar( col = data$colour, fill = fill_alpha(data$fill, data$alpha), - lwd = data$linewidth * .pt, + lwd = data$linewidth, lineend = lineend, linejoin = linejoin, linemitre = linemitre diff --git a/R/geom-path.R b/R/geom-path.R index ad7589a028..b705162b9f 100644 --- a/R/geom-path.R +++ b/R/geom-path.R @@ -200,10 +200,10 @@ GeomPath <- ggproto("GeomPath", Geom, segmentsGrob( munched$x[!end], munched$y[!end], munched$x[!start], munched$y[!start], default.units = "native", arrow = arrow, - gp = gpar( + gp = ggpar( col = alpha(munched$colour, munched$alpha)[!end], fill = alpha(munched$colour, munched$alpha)[!end], - lwd = munched$linewidth[!end] * .pt, + lwd = munched$linewidth[!end], lty = munched$linetype[!end], lineend = lineend, linejoin = linejoin, @@ -215,10 +215,10 @@ GeomPath <- ggproto("GeomPath", Geom, polylineGrob( munched$x, munched$y, id = id, default.units = "native", arrow = arrow, - gp = gpar( + gp = ggpar( col = alpha(munched$colour, munched$alpha)[start], fill = alpha(munched$colour, munched$alpha)[start], - lwd = munched$linewidth[start] * .pt, + lwd = munched$linewidth[start], lty = munched$linetype[start], lineend = lineend, linejoin = linejoin, diff --git a/R/geom-point.R b/R/geom-point.R index 20a7f46b58..0de4a68173 100644 --- a/R/geom-point.R +++ b/R/geom-point.R @@ -145,18 +145,15 @@ GeomPoint <- ggproto("GeomPoint", Geom, } coords <- coord$transform(data, panel_params) - stroke_size <- coords$stroke - stroke_size[is.na(stroke_size)] <- 0 ggname("geom_point", pointsGrob( coords$x, coords$y, pch = coords$shape, - gp = gpar( + gp = ggpar( col = alpha(coords$colour, coords$alpha), fill = fill_alpha(coords$fill, coords$alpha), - # Stroke is added around the outside of the point - fontsize = coords$size * .pt + stroke_size * .stroke / 2, - lwd = coords$stroke * .stroke / 2 + pointsize = coords$size, + stroke = coords$stroke ) ) ) diff --git a/R/geom-polygon.R b/R/geom-polygon.R index c644d9daad..3cdce9b53e 100644 --- a/R/geom-polygon.R +++ b/R/geom-polygon.R @@ -130,10 +130,10 @@ GeomPolygon <- ggproto("GeomPolygon", Geom, polygonGrob( munched$x, munched$y, default.units = "native", id = munched$group, - gp = gpar( + gp = ggpar( col = first_rows$colour, fill = fill_alpha(first_rows$fill, first_rows$alpha), - lwd = first_rows$linewidth * .pt, + lwd = first_rows$linewidth, lty = first_rows$linetype, lineend = lineend, linejoin = linejoin, @@ -161,10 +161,10 @@ GeomPolygon <- ggproto("GeomPolygon", Geom, munched$x, munched$y, default.units = "native", id = id, pathId = munched$group, rule = rule, - gp = gpar( + gp = ggpar( col = first_rows$colour, fill = fill_alpha(first_rows$fill, first_rows$alpha), - lwd = first_rows$linewidth * .pt, + lwd = first_rows$linewidth, lty = first_rows$linetype, lineend = lineend, linejoin = linejoin, diff --git a/R/geom-rect.R b/R/geom-rect.R index d39978897a..cfd39dab4a 100644 --- a/R/geom-rect.R +++ b/R/geom-rect.R @@ -57,10 +57,10 @@ GeomRect <- ggproto("GeomRect", Geom, height = coords$ymax - coords$ymin, default.units = "native", just = c("left", "top"), - gp = gpar( + gp = ggpar( col = coords$colour, fill = fill_alpha(coords$fill, coords$alpha), - lwd = coords$linewidth * .pt, + lwd = coords$linewidth, lty = coords$linetype, linejoin = linejoin, lineend = lineend diff --git a/R/geom-ribbon.R b/R/geom-ribbon.R index d93df77850..11774c97f5 100644 --- a/R/geom-ribbon.R +++ b/R/geom-ribbon.R @@ -182,10 +182,10 @@ GeomRibbon <- ggproto("GeomRibbon", Geom, g_poly <- polygonGrob( munched_poly$x, munched_poly$y, id = munched_poly$id, default.units = "native", - gp = gpar( + gp = ggpar( fill = fill_alpha(aes$fill, aes$alpha), col = if (is_full_outline) aes$colour else NA, - lwd = if (is_full_outline) aes$linewidth * .pt else 0, + lwd = if (is_full_outline) aes$linewidth else 0, lty = if (is_full_outline) aes$linetype else 1, lineend = lineend, linejoin = linejoin, @@ -213,9 +213,9 @@ GeomRibbon <- ggproto("GeomRibbon", Geom, g_lines <- polylineGrob( munched_lines$x, munched_lines$y, id = munched_lines$id, default.units = "native", - gp = gpar( + gp = ggpar( col = aes$colour, - lwd = aes$linewidth * .pt, + lwd = aes$linewidth, lty = aes$linetype, lineend = lineend, linejoin = linejoin, diff --git a/R/geom-rug.R b/R/geom-rug.R index 0fe393bb95..cda6a01bc4 100644 --- a/R/geom-rug.R +++ b/R/geom-rug.R @@ -108,10 +108,10 @@ GeomRug <- ggproto("GeomRug", Geom, list(min = -1 * length, max = unit(1, "npc") + length) } - gp <- gpar( + gp <- ggpar( col = alpha(data$colour, data$alpha), lty = data$linetype, - lwd = data$linewidth * .pt, + lwd = data$linewidth, lineend = lineend ) if (!is.null(data$x)) { diff --git a/R/geom-segment.R b/R/geom-segment.R index f32b61f876..9a4de4dbb3 100644 --- a/R/geom-segment.R +++ b/R/geom-segment.R @@ -122,10 +122,10 @@ GeomSegment <- ggproto("GeomSegment", Geom, arrow.fill <- arrow.fill %||% coord$colour return(segmentsGrob(coord$x, coord$y, coord$xend, coord$yend, default.units = "native", - gp = gpar( + gp = ggpar( col = alpha(coord$colour, coord$alpha), fill = alpha(arrow.fill, coord$alpha), - lwd = coord$linewidth * .pt, + lwd = coord$linewidth, lty = coord$linetype, lineend = lineend, linejoin = linejoin diff --git a/R/geom-text.R b/R/geom-text.R index acfbb0337a..ea996cb040 100644 --- a/R/geom-text.R +++ b/R/geom-text.R @@ -239,7 +239,7 @@ GeomText <- ggproto("GeomText", Geom, data$x, data$y, default.units = "native", hjust = data$hjust, vjust = data$vjust, rot = data$angle, - gp = gpar( + gp = ggpar( col = alpha(data$colour, data$alpha), fontsize = data$size * size.unit, fontfamily = data$family, diff --git a/R/guide-colorbar.R b/R/guide-colorbar.R index b8e62f82c9..6d90f13a82 100644 --- a/R/guide-colorbar.R +++ b/R/guide-colorbar.R @@ -379,7 +379,7 @@ GuideColourbar <- ggproto( vjust = 0, hjust = 0, width = width, height = height, default.units = "npc", - gp = gpar(col = NA, fill = decor$colour) + gp = ggpar(col = NA, fill = decor$colour) ) } else if (params$display == "gradient") { check_device("gradients", call = expr(guide_colourbar())) @@ -394,7 +394,7 @@ GuideColourbar <- ggproto( vertical = list(x1 = unit(0.5, "npc"), x2 = unit(0.5, "npc")) ) gradient <- inject(linearGradient(decor$colour, value, !!!position)) - grob <- rectGrob(gp = gpar(fill = gradient, col = NA)) + grob <- rectGrob(gp = ggpar(fill = gradient, col = NA)) } frame <- element_grob(elements$frame, fill = NA) diff --git a/R/guide-colorsteps.R b/R/guide-colorsteps.R index d85421bbc9..cd21dce6a4 100644 --- a/R/guide-colorsteps.R +++ b/R/guide-colorsteps.R @@ -204,7 +204,7 @@ GuideColoursteps <- ggproto( size <- abs(decor$max - decor$min) just <- as.numeric(decor$min > decor$max) - gp <- gpar(col = NA, fill = decor$colour) + gp <- ggpar(col = NA, fill = decor$colour) if (params$direction == "vertical") { grob <- rectGrob( x = 0, y = decor$min, diff --git a/R/legend-draw.R b/R/legend-draw.R index f1de0b80e5..ce16f7a6ab 100644 --- a/R/legend-draw.R +++ b/R/legend-draw.R @@ -31,16 +31,13 @@ draw_key_point <- function(data, params, size) { } # NULL means the default stroke size, and NA means no stroke. - stroke_size <- data$stroke %||% 0.5 - stroke_size[is.na(stroke_size)] <- 0 - pointsGrob(0.5, 0.5, pch = data$shape, - gp = gpar( + gp = ggpar( col = alpha(data$colour %||% "black", data$alpha), fill = fill_alpha(data$fill %||% "black", data$alpha), - fontsize = (data$size %||% 1.5) * .pt + stroke_size * .stroke / 2, - lwd = stroke_size * .stroke / 2 + pointsize = data$size %||% 1.5, + stroke = data$stroke %||% 0.5 ) ) } @@ -49,9 +46,9 @@ draw_key_point <- function(data, params, size) { #' @rdname draw_key draw_key_abline <- function(data, params, size) { segmentsGrob(0, 0, 1, 1, - gp = gpar( + gp = ggpar( col = alpha(data$colour %||% data$fill %||% "black", data$alpha), - lwd = (data$linewidth %||% 0.5) * .pt, + lwd = data$linewidth %||% 0.5, lty = data$linetype %||% 1, lineend = params$lineend %||% "butt" ) @@ -61,7 +58,7 @@ draw_key_abline <- function(data, params, size) { #' @export #' @rdname draw_key draw_key_rect <- function(data, params, size) { - rectGrob(gp = gpar( + rectGrob(gp = ggpar( col = NA, fill = fill_alpha(data$fill %||% data$colour %||% "grey20", data$alpha), lty = data$linetype %||% 1 @@ -79,11 +76,11 @@ draw_key_polygon <- function(data, params, size) { rectGrob( width = unit(1, "npc") - unit(lwd, "mm"), height = unit(1, "npc") - unit(lwd, "mm"), - gp = gpar( + gp = ggpar( col = data$colour %||% NA, fill = fill_alpha(data$fill %||% "grey20", data$alpha), lty = data$linetype %||% 1, - lwd = lwd * .pt, + lwd = lwd, linejoin = params$linejoin %||% "mitre", lineend = params$lineend %||% "butt" )) @@ -98,10 +95,10 @@ draw_key_blank <- function(data, params, size) { #' @export #' @rdname draw_key draw_key_boxplot <- function(data, params, size) { - gp <- gpar( + gp <- ggpar( col = data$colour %||% "grey20", fill = fill_alpha(data$fill %||% "white", data$alpha), - lwd = (data$linewidth %||% 0.5) * .pt, + lwd = data$linewidth %||% 0.5, lty = data$linetype %||% 1, lineend = params$lineend %||% "butt", linejoin = params$linejoin %||% "mitre" @@ -129,10 +126,10 @@ draw_key_boxplot <- function(data, params, size) { #' @export #' @rdname draw_key draw_key_crossbar <- function(data, params, size) { - gp <- gpar( + gp <- ggpar( col = data$colour %||% "grey20", fill = fill_alpha(data$fill %||% "white", data$alpha), - lwd = (data$linewidth %||% 0.5) * .pt, + lwd = data$linewidth %||% 0.5, lty = data$linetype %||% 1, lineend = params$lineend %||% "butt", linejoin = params$linejoin %||% "mitre" @@ -161,11 +158,11 @@ draw_key_path <- function(data, params, size) { data$linetype[is.na(data$linetype)] <- 0 } grob <- segmentsGrob(0.1, 0.5, 0.9, 0.5, - gp = gpar( + gp = ggpar( col = alpha(data$colour %||% data$fill %||% "black", data$alpha), fill = alpha(params$arrow.fill %||% data$colour %||% data$fill %||% "black", data$alpha), - lwd = (data$linewidth %||% 0.5) * .pt, + lwd = data$linewidth %||% 0.5, lty = data$linetype %||% 1, lineend = params$lineend %||% "butt" ), @@ -184,9 +181,9 @@ draw_key_path <- function(data, params, size) { #' @rdname draw_key draw_key_vpath <- function(data, params, size) { grob <- segmentsGrob(0.5, 0.1, 0.5, 0.9, - gp = gpar( + gp = ggpar( col = alpha(data$colour %||% data$fill %||% "black", data$alpha), - lwd = (data$linewidth %||% 0.5) * .pt, + lwd = data$linewidth %||% 0.5, lty = data$linetype %||% 1, lineend = params$lineend %||% "butt" ), @@ -206,7 +203,7 @@ draw_key_vpath <- function(data, params, size) { draw_key_dotplot <- function(data, params, size) { pointsGrob(0.5, 0.5, size = unit(.5, "npc"), pch = 21, - gp = gpar( + gp = ggpar( col = alpha(data$colour %||% "black", data$alpha), fill = fill_alpha(data$fill %||% "black", data$alpha), lty = data$linetype %||% 1, @@ -247,7 +244,7 @@ draw_key_smooth <- function(data, params, size) { path <- draw_key_path(data, params, size) grob <- grobTree( - if (isTRUE(params$se)) rectGrob(gp = gpar(col = NA, fill = data$fill)), + if (isTRUE(params$se)) rectGrob(gp = ggpar(col = NA, fill = data$fill)), path ) attr(grob, "width") <- attr(path, "width") @@ -268,7 +265,7 @@ draw_key_text <- function(data, params, size) { angle = data$angle, hjust = hjust, vjust = vjust, - gp = gpar( + gp = ggpar( col = alpha(data$colour %||% data$fill %||% "black", data$alpha), fontfamily = data$family %||% "", fontface = data$fontface %||% 1, @@ -304,16 +301,16 @@ draw_key_label <- function(data, params, size) { just = c(hjust, vjust), padding = padding, r = params$label.r %||% unit(0.15, "lines"), - text.gp = gpar( + text.gp = ggpar( col = data$colour %||% "black", fontfamily = data$family %||% "", fontface = data$fontface %||% 1, fontsize = (data$size %||% 3.88) * .pt ), - rect.gp = gpar( + rect.gp = ggpar( col = if (isTRUE(all.equal(params$label.size, 0))) NA else data$colour, fill = alpha(data$fill %||% "white", data$alpha), - lwd = params$label.size * .pt + lwd = params$label.size ) ) angle <- deg2rad(data$angle %||% 0) @@ -331,9 +328,9 @@ draw_key_label <- function(data, params, size) { #' @rdname draw_key draw_key_vline <- function(data, params, size) { segmentsGrob(0.5, 0, 0.5, 1, - gp = gpar( + gp = ggpar( col = alpha(data$colour %||% data$fill %||% "black", data$alpha), - lwd = (data$linewidth %||% 0.5) * .pt, + lwd = data$linewidth %||% 0.5, lty = data$linetype %||% 1, lineend = params$lineend %||% "butt" ) @@ -352,9 +349,9 @@ draw_key_timeseries <- function(data, params, size) { grid::linesGrob( x = c(0, 0.4, 0.6, 1), y = c(0.1, 0.6, 0.4, 0.9), - gp = gpar( + gp = ggpar( col = alpha(data$colour %||% data$fill %||% "black", data$alpha), - lwd = (data$linewidth %||% 0.5) * .pt, + lwd = data$linewidth %||% 0.5, lty = data$linetype %||% 1, lineend = params$lineend %||% "butt", linejoin = params$linejoin %||% "round" diff --git a/R/margins.R b/R/margins.R index b563331002..c1ad607a21 100644 --- a/R/margins.R +++ b/R/margins.R @@ -124,9 +124,9 @@ titleGrob <- function(label, x, y, hjust, vjust, angle = 0, gp = gpar(), rectGrob( x = x, y = y, width = width, height = height, hjust = just$hjust, vjust = just$vjust, - gp = gpar(fill = "cornsilk", col = NA) + gp = ggpar(fill = "cornsilk", col = NA) ), - pointsGrob(x, y, pch = 20, gp = gpar(col = "gold")), + pointsGrob(x, y, pch = 20, gp = ggpar(col = "gold")), grob ) } else { @@ -193,7 +193,7 @@ justify_grobs <- function(grobs, x = NULL, y = NULL, hjust = 0.5, vjust = 0.5, if (isTRUE(debug)) { children <- gList( - rectGrob(gp = gpar(fill = "lightcyan", col = NA)), + rectGrob(gp = ggpar(fill = "lightcyan", col = NA)), grobs ) } @@ -219,7 +219,7 @@ justify_grobs <- function(grobs, x = NULL, y = NULL, hjust = 0.5, vjust = 0.5, #cat("E - hjust, vjust:", c(hjust, vjust), "\n") grobTree( result_grob, - pointsGrob(x, y, pch = 20, gp = gpar(col = "mediumturquoise")) + pointsGrob(x, y, pch = 20, gp = ggpar(col = "mediumturquoise")) ) } else { result_grob @@ -307,7 +307,7 @@ font_descent <- function(family = "", face = "plain", size = 12, cex = 1) { if (is.null(descent)) { descent <- convertHeight(grobDescent(textGrob( label = "gjpqyQ", - gp = gpar( + gp = ggpar( fontsize = size, cex = cex, fontfamily = family, diff --git a/R/theme-elements.R b/R/theme-elements.R index b8e83c75e4..41b989df7e 100644 --- a/R/theme-elements.R +++ b/R/theme-elements.R @@ -187,14 +187,6 @@ element_render <- function(theme, element, ..., name = NULL) { ggname(paste(element, name, sep = "."), grob) } - -# Returns NULL if x is length 0 -len0_null <- function(x) { - if (length(x) == 0) NULL - else x -} - - #' Generate grid grob from theme element #' #' @param element Theme element, i.e. `element_rect` or similar. @@ -220,8 +212,8 @@ element_grob.element_rect <- function(element, x = 0.5, y = 0.5, } # The gp settings can override element_gp - gp <- gpar(lwd = len0_null(linewidth * .pt), col = colour, fill = fill, lty = linetype) - element_gp <- gpar(lwd = len0_null(element$linewidth * .pt), col = element$colour, + gp <- ggpar(lwd = linewidth, col = colour, fill = fill, lty = linetype) + element_gp <- ggpar(lwd = element$linewidth, col = element$colour, fill = element$fill, lty = element$linetype) rectGrob(x, y, width, height, gp = modify_list(element_gp, gp), ...) @@ -244,10 +236,10 @@ element_grob.element_text <- function(element, label = "", x = NULL, y = NULL, angle <- angle %||% element$angle %||% 0 # The gp settings can override element_gp - gp <- gpar(fontsize = size, col = colour, + gp <- ggpar(fontsize = size, col = colour, fontfamily = family, fontface = face, lineheight = lineheight) - element_gp <- gpar(fontsize = element$size, col = element$colour, + element_gp <- ggpar(fontsize = element$size, col = element$colour, fontfamily = element$family, fontface = element$face, lineheight = element$lineheight) @@ -269,13 +261,13 @@ element_grob.element_line <- function(element, x = 0:1, y = 0:1, } # The gp settings can override element_gp - gp <- gpar( + gp <- ggpar( col = colour, fill = colour, - lwd = len0_null(linewidth * .pt), lty = linetype, lineend = lineend + lwd = linewidth, lty = linetype, lineend = lineend ) - element_gp <- gpar( + element_gp <- ggpar( col = element$colour, fill = element$colour, - lwd = len0_null(element$linewidth * .pt), lty = element$linetype, + lwd = element$linewidth, lty = element$linetype, lineend = element$lineend ) arrow <- if (is.logical(element$arrow) && !element$arrow) { diff --git a/R/utilities-grid.R b/R/utilities-grid.R index 389dad3eea..8efdee4d02 100644 --- a/R/utilities-grid.R +++ b/R/utilities-grid.R @@ -13,6 +13,38 @@ ggname <- function(prefix, grob) { grob } +#' Interpreter for graphical parameters +#' +#' This is a wrapper for [`grid::gpar()`] that applies ggplot2's interpretation +#' of graphical parameters. +#' +#' @param ... Named arguments passed on to `gpar()`. +#' @param stroke Linewidth for points. Populates the `lwd` grid parameter. +#' @param pointsize Size for points. Populates the `fontsize` grid parameter. +#' +#' @return An object of class 'gpar'. +#' @keywords internal +#' @export +ggpar <- function(..., stroke = NULL, pointsize = NULL) { + args <- list2(...) + args <- args[lengths(args) > 0] + + if (!is.null(args$lwd)) { + args$lwd <- args$lwd * .pt + } + if (!is.null(stroke)) { + args$lwd <- stroke * .stroke / 2 + } + if (!is.null(pointsize)) { + # Stroke is added around the outside of the point + stroke <- stroke %||% 0 + stroke[is.na(stroke)] <- 0 + args$fontsize <- pointsize * .pt + stroke * .stroke / 2 + } + + inject(gpar(!!!args)) +} + width_cm <- function(x) { if (is.grob(x)) { convertWidth(grobWidth(x), "cm", TRUE) diff --git a/man/ggpar.Rd b/man/ggpar.Rd new file mode 100644 index 0000000000..3186597d02 --- /dev/null +++ b/man/ggpar.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utilities-grid.R +\name{ggpar} +\alias{ggpar} +\title{Interpreter for graphical parameters} +\usage{ +ggpar(..., stroke = NULL, pointsize = NULL) +} +\arguments{ +\item{...}{Named arguments passed on to \code{gpar()}.} + +\item{stroke}{Linewidth for points. Populates the \code{lwd} grid parameter.} + +\item{pointsize}{Size for points. Populates the \code{fontsize} grid parameter.} +} +\value{ +An object of class 'gpar'. +} +\description{ +This is a wrapper for \code{\link[grid:gpar]{grid::gpar()}} that applies ggplot2's interpretation +of graphical parameters. +} +\keyword{internal}