I have a ggplot map, for example:
library(ggmap)
ggmap(get_map())
I'd like the axis labels to be automatically labeled as N-S / W-E: in the above case, for example, instead of lon -95.4 it should show 95.4°E.
I have tried to mess with the scales
package and using scale_x_continuous
and scale_y_continuous
labels and breaks options, but I have not managed to make it work.
It would be awesome to have a scale_y_latitude
and scale_x_longitude
.
EDIT: Thanks to @Jaap 's answer I got to the following:
scale_x_longitude <- function(xmin=-180, xmax=180, step=1, ...) {
ewbrks <- seq(xmin,xmax,step)
ewlbls <- unlist(lapply(ewbrks, function(x) ifelse(x < 0, paste(x, "W"), ifelse(x > 0, paste(x, "E"),x))))
return(scale_x_continuous("Longitude", breaks = ewbrks, labels = ewlbls, expand = c(0, 0), ...))
}
scale_y_latitude <- function(ymin=-90, ymax=90, step=0.5, ...) {
nsbrks <- seq(ymin,ymax,step)
nslbls <- unlist(lapply(nsbrks, function(x) ifelse(x < 0, paste(x, "S"), ifelse(x > 0, paste(x, "N"),x))))
return(scale_y_continuous("Latitude", breaks = nsbrks, labels = nslbls, expand = c(0, 0), ...))
}
Which works pretty well. But for some reason my R doesn't seem to like the degree symbol in front of the cardinal point... It is displayed as a simple dot, e.g. longitude -24 becomes 24..W
library(scales); p + scale_y_continuous(labels = percent)
(see docs.ggplot2.org/current/scale_continuous.html)... by the way my R doesn't seem to like the degree symbol in "°E"... it is not rendered correctly for some reason. Ideas? – Flipflop