1

I'm trying to save a map with coordinates and labels (using ggrepel) as an image file.

This is my code:

library(ggplot2)
library(ggmap)
library(ggrepel)
library(grid)

lon = c(8.5213, 9.9576, 11.0621, 12.1844, 9.1307, 8.4090, 11.0549, 9.0997, 
        9.8648, 12.5024, 7.8343, 10.9308, 11.8134, 10.0792, 8.5585, 10.2759, 
        12.1019, 9.2235,10.3348, 10.9206)


lat = c(50.0259, 49.7704, 47.4830, 49.6662, 47.6952, 48.4538, 49.5030, 49.7177, 
        48.6656, 48.2790, 48.0232, 49.0115, 48.3477, 50.2240, 49.5062, 47.3984,
        49.0425, 48.6883, 47.7233, 49.8743)

labels = c("Frankfurt/Main","Würzburg","Garmisch-Partenkirchen","Weiden",
           "Konstanz", "Freudenstadt","Nürnberg","Michelstadt-Vielbrunn",
           "Stötten","Mühldorf", "Freiburg","Weißenburg-Emetzheim",
           "München-Flughafen","Kissingen, Bad","Mannheim","Oberstdorf",
           "Regensburg","Stuttgart-Echterdingen","Kempten","Bamberg")


df <- data.frame(lat,lon,labels)

sg_box = c(left=floor(min(lon)),bottom=floor(min(lat)),right=ceiling(max(lon)),
           top=ceiling(max(lat)))

sg_sm = get_stamenmap(bbox=sg_box,zoom=7,maptype="terrain-background")

sg_map =
  ggmap(sg_sm, extent = "device") +
  geom_point(data = df, aes(x = lon, y = lat), 
             alpha = 0.8, fill = "red", pch = 21, size = 2) + 
  labs(x = 'Longitude', y = 'Latitude') +
  geom_label_repel(data = df, aes(x = lon, y = lat, label = labels), 
                   fill = "white", box.padding = 0.35,
                   label.padding = 0.15,
                   segment.color = "red", segment.size = 0.5)

ggsave(sg_map,filename="sg_stations.png",device="png",units="px",width=1600,
       height=1200)

When I run this script warning messages appear

Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) : no non-missing arguments to max; returning -Inf

Further more this prevents the map from being saved as an image file with ggsave().

Edit:

Trying this with RStudio works, but the logitude-latitude axis is missing.

enter image description here

AlexLee
  • 429
  • 4
  • 11
  • Using your code, I can plot and save the map OK (except the warnings). Did you by any chance save the image to an unexpected place (check `getwd()`)? –  May 20 '22 at 14:02
  • The longitude and latitude axis is missing though – AlexLee May 20 '22 at 14:07
  • 1
    Try setting the extent to "normal": `ggmap(sg_sm, extent = "normal") # + ...` –  May 20 '22 at 14:27
  • That seems to work, the question is now is how to get rid of the grey bits around the map. Thanks! – AlexLee May 23 '22 at 09:51
  • You can manually change the `theme()` settings (https://ggplot2.tidyverse.org/reference/theme.html) or use a predefinded theme (https://r-charts.com/ggplot2/themes/). `ggplot() + ... + theme_classic()` might be a candidate. –  May 23 '22 at 10:24
  • ... and to remove the whitespace between map and axes, set the expansion of the axes' limits to zero: `ggplot + ... + scale_x_continuous(expand = c(0,0)) + scale_y_continuous(expand = c(0,0)) + ...` –  May 23 '22 at 11:00

0 Answers0