Similar to ggplot2: How to rotate a graph in a specific angle?, but I don’t want the image/square rotated, I’d like the data rotated within the frame.
For instance, if I start with this:
library(ggplot2)
usa <- maps::map('usa', plot=FALSE)
ggplot(as.data.frame(usa[c("x","y")]), aes(x,y)) +
coord_quickmap() +
geom_path()
I’d like to be able to generate this:
- Rotation needs to preserve aspect-ratio of the spatial data
- I need to support arbitrary rotation, not constrained by 90-degree jumps
- I’d like if the actual grid can be retained (to preserve the default behaviors within
ggplot2
), but if not I can generate them manually - I’d like the enclosing rectangle of the generic plot to be unchanged, including (not shown here) titles and subtitles
- I’d like it if the coordinate system is unchanged, meaning that I can add other layers that will subsequently automatically appreciate the rotation. @qdread’s answer gives the alternative to this, where a single function can calculate the rotation (though I don’t know how to do this mathematically with spatial data)
- Please disregard:
- the grid problems in the top-left corner, my photoshopping was incomplete
- the x-axis labels, the numbers are wrong (y-axis is good-ish) (they can be removed, not required)
I wonder if this can be accomplished with CRS
/projections, but I’m not smart enough on them to work with it formally/correctly.
2
Answers
In order to preserve the shape of the map when we rotate, we first need to transform from lat-long to a conformal coordinate system where local angles are preserved. We will use a Lambert conformal conic projection, specifically ESRI:102004 for contiguous USA. We coerce the
usa
object to asf
object and apply the CRS transformation.The result looks like this:
Next we modify the procedure in the sf documentation on affine transformations to rotate the geometry around its centroid.
The following function defines the transformation matrix.
Extract the geometry from the transformed object and then get the centroid. This will return ten points because there are 9 large islands included in addition to the mainland. (e.g. Long Island).
Therefore we take
usa_centroid[1,]
which is the centroid of the mainland polygon, subtract it, apply the rotation of 45 degrees counterclockwise, and add back the centroid.The result looks like this:
Finally if desired you can back-transform to lat-long again before plotting.
Result:
edit2:
Using the oblique mercator projection to rotate a map:
Animation of gamma from 0:360 in 10deg increments, alpha constant at 0. The artifacts are from gif compression, actual plots look like the one above labelled gamma 45.
earlier answer:
I think you can ‘rotate’ the plot (including graticules) by ‘looking’ at the earth from a different perspective by changing the projection to a Lambert azimuthal equal area and adjusting
+lon_0=x
in the projection string.This should meet most of your goals, but I don’t know how to get an exact rotation in degrees.
Below I’ve transformed the
states_sf
sf object manually before plotting. It may be easier to transform the plot (and all the sf data being plotted) by working with crs 4326 for the data, and adding+ coord_sf(crs = "+proj=laea +x_0=0 +y_0=0 +lon_0=-140 +lat_0=40")
to the end of theggplot() +
call.Created on 2021-03-31 by the reprex package (v0.3.0)
edit:
It looks like you can get any angle with a combination of alpha and gamma when using the projection
"+proj=omerc +lonc=-90 +lat_0=40 +gamma=0 +alpha=0"
. I don’t know exactly how they relate (something to do with azimuths), but this should help visualize it:Animation of a broader range of alpha & gamma can be found here.