16EAGLE / basemaps

A lightweight package for accessing basemaps from open sources in R 🗺️
https://jakob.schwalb-willmann.de/basemaps
GNU General Public License v3.0
57 stars 15 forks source link

Stubborn white border around maps. #4

Closed admahood closed 3 years ago

admahood commented 3 years ago

In ggplot, there is a border of blank space when I use basemaps::basemap_gglayer or basemaps::basemap_ggplot. No matter what I do, the map refuses to go all the way to the edge of the plot border. Any way to get rid of this? it basically prevents me from being able to use this package for my visualizations. Other than this, I love the package!

Rplot

16EAGLE commented 3 years ago

Thanks for trying out basemaps. Not sure since I don't see your code but I guess this stems from ggplots axis expansion behavior which is the default for any ggplot. Here a reproducible example for a basemap without the expansion:

library(ggplot2)
library(basemaps)

data(ext)
set_defaults(map_service = "osm", map_type = "topographic")

gg <- basemap_ggplot(ext)

#default ggplot2 behavior
gg

plot1

# with expanion reduced to 0 for both axis
gg + scale_x_continuous(expand = c(0,0)) + scale_y_continuous(expand = c(0,0)) 

plot2

Does that solve the issue?

16EAGLE commented 3 years ago

And in case the issue is regarding ggplot2 plot margins and their removal, here a minimal example of the same map without any white space around it:

gg + scale_x_continuous(expand = c(0,0)) + scale_y_continuous(expand = c(0,0)) + 
  labs(x=NULL, y=NULL) +
  theme(axis.line = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        axis.title = element_blank(),
        axis.ticks.length = unit(0, "pt"), #length of tick marks
        legend.position = "none",
        panel.background = element_blank(),
        panel.border = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        plot.background = element_blank(), 
        plot.margin = unit(c(0,0,0,0),"mm"))

plot3

admahood commented 3 years ago

I think that solves it. Thanks!