Skip to content

Commit

Permalink
Use grid::roundrectGrob. Tweak adjustment to projection lines to be "…
Browse files Browse the repository at this point in the history
…looser".
  • Loading branch information
hughjonesd committed Jan 31, 2024
1 parent eea90c4 commit 8cf46a3
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 22 deletions.
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ Depends:
R (>= 4.1.0)
Imports:
cli,
ggforce (>= 0.4.1.9000),
ggplot2 (>= 3.3.4),
grid,
gridExtra,
Expand Down
4 changes: 2 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

# ggmagnify (development version)

* New `corners` argument sets corner radius on target and inset.
- We now depend on the "ggforce" package.
* New `corners` argument sets corner radius on target and inset. Thanks
to @thomasp85 for help with this.

# ggmagnify 0.2.0

Expand Down
3 changes: 1 addition & 2 deletions R/compute-shape-grob.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ compute_shape_grob.data.frame <- function (from, shape, corners, data, coord,
compute_shape_grob.default <- function (from, shape, corners, data, coord,
panel_params, expand) {
if (shape == "rect") {
ggforce::shapeGrob(x = c(0, 0, 1, 1),
y = c(0, 1, 1, 0), radius = corners)
grid::roundrectGrob(r = unit(corners, "snpc"))
} else if (shape == "ellipse") {
# resist the temptation to replace this with circleGrob. You need to
# mess with it later.
Expand Down
3 changes: 2 additions & 1 deletion R/geom-magnify.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ NULL
#' @param shadow Logical. Draw a shadow behind the inset plot? Requires the
#' "ggfx" package.
#' @param corners Numeric between 0 and 1. Radius of rounded corners for the
#' target area and inset. Only used if `shape` is `"rect"`.
#' target area and inset. Only used if `shape` is `"rect"`. 0.1 is a good
#' starting value.
#' @param linetype,colour,alpha,linewidth Linetype, colour, alpha and linewidth
#' for borders and projection lines.
#' @param target.linetype,inset.linetype,proj.linetype Linetypes
Expand Down
36 changes: 21 additions & 15 deletions R/projection.R
Original file line number Diff line number Diff line change
Expand Up @@ -163,22 +163,28 @@ calculate_proj_df_rect <- function(proj, data, corners, coord, panel_params) {
to_ymax <- data$to_ymax

if (corners > 0 && ! identical(proj, "single")) {
# the 1 - 1/sqrt(2) adjustment gets to the midpoint of the
# a 1 - 1/sqrt(2) adjustment, c. 0.29, would get to the midpoint of the
# rounded corner (at a 45 degree angle), calculated via Pythagoras.
# Not perfect.
adj <- 1 - 1/sqrt(2)
corn_adj_x <- corners * (xmax - xmin) * adj
corn_adj_y <- corners * (ymax - ymin) * adj
to_corn_adj_x <- corners * (to_xmax - to_xmin) * adj
to_corn_adj_y <- corners * (to_ymax - to_ymin) * adj
xmin <- xmin + corn_adj_x
xmax <- xmax - corn_adj_x
ymin <- ymin + corn_adj_y
ymax <- ymax - corn_adj_y
to_xmin <- to_xmin + to_corn_adj_x
to_xmax <- to_xmax - to_corn_adj_x
to_ymin <- to_ymin + to_corn_adj_y
to_ymax <- to_ymax - to_corn_adj_y
# This isn't perfect because lines don't always come in at 45 deg.
# An adj of 0.2 is a bit "looser" and should give fewer cases
# where projection lines end up inside the target area.
# The use of min() below reflects that corners is in "snpc" units:
# see ?grid::unit.
size <- min(xmax - xmin, ymax - ymin)
to_size <- min(to_xmax - to_xmin, to_ymax - to_ymin)
adj <- 0.2 # 1 - 1/sqrt(2)
corn_adj <- corners * size * adj
to_corn_adj <- corners * to_size * adj

xmin <- xmin + corn_adj
xmax <- xmax - corn_adj
ymin <- ymin + corn_adj
ymax <- ymax - corn_adj

to_xmin <- to_xmin + to_corn_adj
to_xmax <- to_xmax - to_corn_adj
to_ymin <- to_ymin + to_corn_adj
to_ymax <- to_ymax - to_corn_adj
}

x <- mean(c(xmin, xmax))
Expand Down
3 changes: 2 additions & 1 deletion man/geom_magnify.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8cf46a3

Please sign in to comment.