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.