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

Interpreter/wrapper for gpar() #5867

Merged
merged 4 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 4 additions & 4 deletions R/annotation-logticks.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
))
}
}
Expand Down Expand Up @@ -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)
))
}
}
Expand Down
4 changes: 2 additions & 2 deletions R/annotation-map.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
},

Expand Down
4 changes: 2 additions & 2 deletions R/coord-radial.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
4 changes: 2 additions & 2 deletions R/coord-sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions R/geom-curve.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions R/geom-dotplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
)
},
Expand Down
4 changes: 2 additions & 2 deletions R/geom-hex.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions R/geom-label.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)
})
Expand All @@ -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.")
Expand All @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions R/geom-map.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions R/geom-path.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
9 changes: 3 additions & 6 deletions R/geom-point.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)
)
Expand Down
8 changes: 4 additions & 4 deletions R/geom-polygon.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions R/geom-rect.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions R/geom-ribbon.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions R/geom-rug.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
4 changes: 2 additions & 2 deletions R/geom-segment.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion R/geom-text.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions R/guide-colorbar.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion R/guide-colorsteps.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading
Loading