Skip to content

Commit

Permalink
Add a test. Adjust projection lines for corners.
Browse files Browse the repository at this point in the history
  • Loading branch information
hughjonesd committed Jan 29, 2024
1 parent cde02f1 commit 6bfa3f3
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
3 changes: 1 addition & 2 deletions R/compute-shape-grob.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,10 @@ 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") {
# grid::rectGrob()
ggforce::shapeGrob(x = c(0, 0, 1, 1),
y = c(0, 1, 1, 0), radius = corners)
} else if (shape == "ellipse") {
# resist the temptation to replace this with cirleGrob. You need to
# resist the temptation to replace this with circleGrob. You need to
# mess with it later.
gridExtra::ellipseGrob(x = 0.5, y = 0.5, size = 0.5, n = 180,
position.units = "npc", size.units = "npc")
Expand Down
3 changes: 2 additions & 1 deletion R/geom-magnify.R
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ GeomMagnify <- ggproto("GeomMagnify", Geom,
# == create projection lines =====
proj_df <- if (identical(shape, "rect") && ! inherits(from, "grob") &&
! inherits(from, "data.frame")) {
calculate_proj_df_rect(proj, d1, coord, panel_params)
calculate_proj_df_rect(proj, d1, corners, coord, panel_params)
} else {
calculate_proj_df(proj, proj.combine, target_grob, border_grob)
}
Expand All @@ -382,6 +382,7 @@ GeomMagnify <- ggproto("GeomMagnify", Geom,
}
)


create_plot_gtable <- function (plot, data, axes, recompute, scale.inset) {
plot_coord <- ggplot_build(plot)$layout$coord
plot_limits <- plot_coord$limits
Expand Down
22 changes: 20 additions & 2 deletions R/projection.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ in_bbox <- function(pts, p1, p2) {
}


calculate_proj_df_rect <- function(proj, data, coord, panel_params) {
calculate_proj_df_rect <- function(proj, data, corners, coord, panel_params) {
# using mean allows Dates and maybe other things
xmin <- data$xmin
xmax <- data$xmax
Expand All @@ -162,13 +162,31 @@ calculate_proj_df_rect <- function(proj, data, coord, panel_params) {
to_ymin <- data$to_ymin
to_ymax <- data$to_ymax

if (corners > 0 && ! identical(proj, "single")) {
# the 1 - 1/sqrt(2) adjustment gets 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
}

x <- mean(c(xmin, xmax))
y <- mean(c(ymin, ymax))
to_x <- mean(c(to_xmin, to_xmax))
to_y <- mean(c(to_ymin, to_ymax))

if (proj %in% c("auto", "corresponding", "facing")) {
if (proj %in% c("corresponding", "facing")) {
# which of the four lines connecting the four corners can we draw?
can_top_left <- sign(xmin - to_xmin) == sign(ymax - to_ymax)
can_bot_right <- sign(xmax - to_xmax) == sign(ymin - to_ymin)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/testthat/test-params.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ test_param("aspect-fixed", aspect = "fixed")
test_param("axes", axes = "xy")
test_param("axes-x", axes = "x")
test_param("axes-y", axes = "y")
test_param("corners", corners = 0.1)
test_param("proj-corresponding", proj = "corresponding")
test_param("proj-single", proj = "single")
test_param("linetype", linetype = 2)
Expand Down

0 comments on commit 6bfa3f3

Please sign in to comment.