I am trying to draw a blue circle on the 66th parallel north. on a map I am making.
I usually fortify()
a SpatialPolygonsDataFrame and then use the combination geom_polygon()
, coord_map("ortho" , ylim = c(50, 90))
and geom_hline()
to achieve this.
However, for a new project I will have to use ggspatial::layer_spatial()
on an object that already has a CRS date, so this straight line drawing and automatic re-projection does not work.
This is a demonstration of my usual approach, which works:
library(tidyverse)
map_data <- rnaturalearth::ne_countries()
map_data_df <- fortify(map_data)
ggplot() +
geom_polygon(data = map_data_df,
aes(x = long,
y = lat,
group = group),
col = "black",
fill = "lightgrey") +
coord_map("ortho" , ylim = c(50, 90)) +
geom_hline(yintercept = 66, col = "blue")
and I am trying to replicate it in this code, but that obviously does not work because the geom_hline()
does not have the right projection:
map_data_transformed <- sp::spTransform(map_data, sp::CRS("+init=epsg:3995"))
# needs some fixing to close all the polygons
map_data_transformed <- rgeos::gBuffer(map_data_transformed, byid=TRUE, width=0)
ggplot() +
ggspatial::layer_spatial(data = map_data_transformed,
fill = "lightgrey", col = "black", size = .2)+
coord_sf( xlim = c(-4500000 , 4500000),
ylim = c(-4500000 , 4500000)) +
geom_hline(yintercept = 66, col = "blue")
See if this works for you?