In R, when plotting a shapefile from 'rnaturalearth', I get white space above and below the map. How can I produce the map without it?
world <- ne_countries(scale = "small", type="map_units",returnclass = "sf")
ggplot() +
geom_sf(
data = world
)
I tried to add various theme settings and + coord_sf(expand = FALSE)
ggplot() +
geom_sf(
data = world
) + ggthemes::theme_map()
ggplot() +
# Base SDI map
geom_sf(
data = world
) + ggthemes::theme_map()
coord_sf(expand = FALSE) +
theme_void() + # Removes ALL margins/padding
theme(
plot.margin = margin(0, 0, 0, 0), # Remove outer plot margins
legend.position = "bottom"
)
It has all that space above and below the map output...
Per comments below, I tried a different approach #Create Object, Then Save
p1<-ggplot() +
geom_sf(
data = world
) + ggthemes::theme_map()
ggsave("testing.jpg", plot = p1, width = 10, height = 6, dpi = 300)

ggsave("testing2.jpg", plot = p1, width = 10, height = 3, dpi = 300)
These are a bit better but not quite right... I need to figure out the dimensions that don't create margins on the left and right or on the top and bottom.
Moving it to an answer. I can not put it better than Allan Cameron so I will quote him here
Surely this is just an issue of the fixed aspect ratio. If you take any image with a fixed aspect ratio, be it a plot or a photograph, and render it on a device that has a different aspect ratio, then you have to have something that fills out the space to the edge of the device. If your plotting window has a higher aspect ratio than the image, you will get white bars at the side of the image. If it has a lower aspect ratio you will get white bars above and below. In this case p1<-ggplot() + geom_sf(data = world) + theme_void() + coord_sf(expand = FALSE); ggsave("testing.jpg", plot = p1, width = 10, height = 5, dpi = 300) works because the aspect ratio of 2:1 approximately matches the aspect ratio of the world map in this projection.
If there is a way to know what the ratio is supposed to be for any particular map aside from trial and error, that would be nice
One could use get_asp_ratio(bb(world)) here to get the aspect ratio (in this case it's 2.070007) of the world map or any map and use it in ggsave or any device you want like
library(rnaturalearth)
library(ggplot2)
world <- ne_countries(scale = "small", type="map_units", returnclass = "sf")
p1 <- ggplot() +
geom_sf(data = world) +
theme_void() +
coord_sf(expand = FALSE)
library(tmaptools)
ggsave("testing.jpg", plot = p1, width = 10 * get_asp_ratio(bb(world)), height = 10, dpi = 300)
giving

More robust against any legends, etc. could be to simply trim any whitespaces from the generated plot-image using image_trim which I remember from this:
library(rnaturalearth)
library(ggplot2)
world <- ne_countries(scale = "small", type="map_units", returnclass = "sf")
p1 <- ggplot() +
geom_sf(data = world, aes(fill = continent)) +
scale_fill_viridis_d(name = "Continent") +
theme_void() +
coord_sf(expand = FALSE)
ggsave("testing.jpg", plot = p1, dpi = 300)
library(magick)
image <- image_read("testing.jpg") |> image_trim()
image_write(image, "testing.jpg")
and that would give

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With