Skip to content

Commit

Permalink
Fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
arcresu committed Jan 16, 2024
1 parent 439e255 commit 3551df3
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 26 deletions.
6 changes: 4 additions & 2 deletions R/geom_inset_frame.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,15 @@ get_outer_bitangents <- function(centre1, radius1, centre2, radius2) {
phi1 <- atan2(centre2[[2]] - centre1[[2]], centre2[[1]] - centre1[[1]]) + acos(short / hypot)
b1 <- sf::st_sfc(
sf::st_point(c(centre1[[1]] + radius1 * cos(phi1), centre1[[2]] + radius1 * sin(phi1))),
sf::st_point(c(centre2[[1]] + radius2 * cos(phi1), centre2[[2]] + radius2 * sin(phi1))))
sf::st_point(c(centre2[[1]] + radius2 * cos(phi1), centre2[[2]] + radius2 * sin(phi1)))
)
b1 <- sf::st_cast(sf::st_union(b1), "LINESTRING")

phi2 <- atan2(centre2[[2]] - centre1[[2]], centre2[[1]] - centre1[[1]]) - acos(short / hypot)
b2 <- sf::st_sfc(
sf::st_point(c(centre1[[1]] + radius1 * cos(phi2), centre1[[2]] + radius1 * sin(phi2))),
sf::st_point(c(centre2[[1]] + radius2 * cos(phi2), centre2[[2]] + radius2 * sin(phi2))))
sf::st_point(c(centre2[[1]] + radius2 * cos(phi2), centre2[[2]] + radius2 * sin(phi2)))
)
b2 <- sf::st_cast(sf::st_union(b2), "LINESTRING")

c(b1, b2)
Expand Down
27 changes: 18 additions & 9 deletions R/geom_sf_inset.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,23 @@ transform_only_viewport <- function(data, inset) {
sf::st_sf(data), inset_centre(inset),
.f = function(data, centre) {
result <- clip_to_viewport(data$geometry, viewport)
geometry <- transform(result[["geometry"]], centre,
scale = inset_scale(inset),
translation = inset_translation(inset))
geometry <- transform(
result[["geometry"]],
centre,
scale = inset_scale(inset),
translation = inset_translation(inset)
)
data <- data[result[["retained"]], ]
data$geometry <- geometry
data
})
}
)

if (nrow(result) == 0 && nrow(data) != 0) {
cli::cli_warn(c("None of the spatial data is inside the inset viewport",
"i" = "Check your inset configuration to ensure the centre, radius, and units are correct"))
cli::cli_warn(c(
"None of the spatial data is inside the inset viewport",
"i" = "Check your inset configuration to ensure the centre, radius, and units are correct"
))
}

result
Expand All @@ -134,11 +140,14 @@ remove_viewport <- function(data, inset) {
data <- data[result[["retained"]], ]
data$geometry <- result[["geometry"]]
data
})
}
)

if (nrow(result) == 0 && nrow(data) != 0) {
cli::cli_warn(c("None of the spatial data is outside the inset viewport",
"i" = "Check your inset configuration to ensure the centre, radius, and units are correct"))
cli::cli_warn(c(
"None of the spatial data is outside the inset viewport",
"i" = "Check your inset configuration to ensure the centre, radius, and units are correct"
))
}

result
Expand Down
3 changes: 2 additions & 1 deletion R/stat_sf_coordinates_inset.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ stat_sf_coordinates_inset <- function(mapping = ggplot2::aes(), data = NULL,
fun.geometry = fun.geometry,
na.rm = na.rm,
...
))
)
)
}

#' @export
Expand Down
6 changes: 4 additions & 2 deletions R/stat_sf_inset.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ stat_sf_inset <- function(mapping = ggplot2::aes(), data = NULL,
inset = inset,
na.rm = na.rm,
...
))
)
)
}

#' @export
Expand Down Expand Up @@ -77,6 +78,7 @@ inset_bbox <- function(inset) {
.f = function(centre) {
viewport <- circular_viewport(centre, radius)
transform(viewport, centre, scale = scale, translation = translation)
})
}
)
sf::st_bbox(result)
}
6 changes: 4 additions & 2 deletions R/transform_to_inset.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ transform_to_inset <- function(x, inset) {
translation <- inset_translation(inset)

result <- with_crs_working(
crs_working, geometry, inset_centre(inset), .f = function(result, centre) {
crs_working, geometry, inset_centre(inset),
.f = function(result, centre) {
transform(result, centre, scale = scale, translation = translation)
})
}
)

if (has_s3_method("st_geometry<-", class(x))) {
sf::st_set_geometry(x, result)
Expand Down
12 changes: 6 additions & 6 deletions data-raw/mozzies_nsw2301.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ sites <- tribble(
"Mullumbimby", "coastal", "-28 33 0", "153 30 0",
"Murrumbidgee", "inland", "-34 48 0", "145 53 0",
"Murwillumbah", "coastal", "-28 19 39", "153 23 45",
"Narrabi","inland", "-30 19 0", "149 46 0",
"Narrendera","inland", "-34 45 0", "146 33 0",
"Narrabi", "inland", "-30 19 0", "149 46 0",
"Narrendera", "inland", "-34 45 0", "146 33 0",
"Newcastle", "coastal", "-32 55 0", "151 45 0",
"Northern Beaches", "sydney", "-33 45 0", "151 17 0",
"Numbucca", "coastal", "-30 43 0", "152 55 0",
Expand All @@ -127,13 +127,13 @@ sites <- tribble(
"Port Macquarie", "coastal", "-31 26 0", "152 54 0",
"Shoalhaven", "coastal", "-34 51 0", "150 44 0",
"Sydney Olympic Park", "sydney", "-33 50 51", "151 03 54",
"Temora","inland", "-34 26 0", "147 32 0",
"Temora", "inland", "-34 26 0", "147 32 0",
"Tweed Heads", "coastal", "-28 11 0", "153 33 0",
"Wagga Wagga", "inland", "-35 7 8", "147 22 8",
"Walgett", "inland", "-30 01 0", "148 07 0",
"Wauchope", "coastal", "-31 27 0", "152 44 0",
"West Wyalong", "inland", "-33 55 0", "147 13 0",
"Wilcannia","inland", "-31 33 25", "143 22 45",
"Wilcannia", "inland", "-31 33 25", "143 22 45",
"Wollongong", "coastal", "-34 25 38", "150 53 38",
"Wyong", "coastal", "-33 16 55", "151 25 5",
"Yass", "inland", "-34 49 0", "148 54 0",
Expand All @@ -149,8 +149,8 @@ coords_to_decimal <- function(coords) {
str_split_fixed(" ", 3) |>
as.integer() |>
matrix(ncol = 3)
sign_coords <- sign(parts[,1])
sign_coords * (abs(parts[,1]) + parts[,2]/60 + parts[,3]/3600)
sign_coords <- sign(parts[, 1])
sign_coords * (abs(parts[, 1]) + parts[, 2] / 60 + parts[, 3] / 3600)
}

expand_levels <- function(raw) {
Expand Down
Binary file modified data/mozzies_nsw2301.rda
Binary file not shown.
12 changes: 8 additions & 4 deletions vignettes/ggmapinset.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ ggplot(nc) +
geom_inset_frame() +
coord_sf_inset(inset = configure_inset(
centre = sf::st_centroid(sf::st_geometry(nc)[nc$NAME == "Bladen"]), scale = 1.5,
translation = c(-180, -50), radius = 50, units = "mi"))
translation = c(-180, -50), radius = 50, units = "mi"
))
```

## Inset frame backgrounds
Expand All @@ -205,7 +206,8 @@ ggplot(nc) +
geom_sf_inset(aes(fill = AREA), map_base = "none") +
coord_sf_inset(inset = configure_inset(
centre = st_centroid(st_geometry(nc)[nc$NAME == "Yancey"]), scale = 2,
translation = c(100, -120), radius = 50, units = "mi"))
translation = c(100, -120), radius = 50, units = "mi"
))
```

## Multiple insets
Expand All @@ -219,10 +221,12 @@ having multiple identical copies of the base map.
```{r multiple, fig.width=7, fig.height=5}
inset1 <- configure_inset(
centre = sf::st_centroid(sf::st_geometry(nc)[nc$NAME == "Bladen"]), scale = 1.5,
translation = c(150, -50), radius = 50, units = "mi")
translation = c(150, -50), radius = 50, units = "mi"
)
inset2 <- configure_inset(
centre = sf::st_centroid(sf::st_geometry(nc)[nc$NAME == "Orange"]), scale = 3,
translation = c(30, 120), radius = 30, units = "mi")
translation = c(30, 120), radius = 30, units = "mi"
)
ggplot(nc) +
# base map
Expand Down

0 comments on commit 3551df3

Please sign in to comment.