5.8 Rotate
# get centroids
= st_centroid(nz_sfc)
nz_centroid_sfc
= function(a){
rotation = a * pi / 180 #degrees to radians
r matrix(c(cos(r), sin(r), -sin(r), cos(r)), nrow = 2, ncol = 2)
}
# rotate by 30 degrees
= (nz_sfc - nz_centroid_sfc) * rotation(30) + nz_centroid_sfc
nz_rotate_sfc
= st_set_geometry(nz, nz_rotate_sfc) #return to sf type
nz_rotate_sf st_crs(nz_rotate_sf) <- st_crs(nz) #ensure same coordinate system
|>
nz ggplot() +
geom_sf() +
geom_sf(fill = "green", alpha = 0.5,
data = nz_rotate_sf) +
labs(title = "New Zealand",
subtitle = "Affine Transformation: Rotate",
caption = "GeoComputation with R book club") +
theme_minimal()
![](images/rotate.gif)
Rotate