north() with ggmap() produces "Error: annotation_custom only works with Cartesian coordinates"
clayms opened this issue · 15 comments
It looks like the annotation_custom
function in the ggplot
package has been replaced with the inset
function in the ggmap
package.
See here.
I copied the entire function to my project, replaced annotation_custom
with inset
, saved as a new function name, and it works great.
There will need to be some conditionals added that will check if the input map is a ggplot
object or a ggmap
object, and then use the appropriate function embedded within the north()
function.
I removed the examples you was reproducing because they are outdated. Please, run the example at the end of the README and let me know if any error occur. I ran it without problems.
The code at the bottom, excerpted from your example, returns a map with the scale-bar as expected when I run it without adding the north(map)
at the end. However, once I add the north(map)
, I receive the following error.
Error in if (rasterRatio > vpRatio) { :
missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In max(data$long) : no non-missing arguments to max; returning -Inf
2: In min(data$long) : no non-missing arguments to min; returning Inf
3: In max(data$lat) : no non-missing arguments to max; returning -Inf
4: In min(data$lat) : no non-missing arguments to min; returning Inf
5: In max(data$long) : no non-missing arguments to max; returning -Inf
6: In max(data$lat) : no non-missing arguments to max; returning -Inf
library(ggsn); library(sf)
dsn <- system.file('extdata', package = 'ggsn')
# Map in geographic coordinates
map <- st_read(dsn, 'sp', quiet = TRUE)
library(ggmap)
library(sp)
library(rgdal)
library(broom)
sp <- get_googlemap("Sao Paulo")
bb <- c(st_bbox(map) * matrix(rep(c(1.001, 0.999), e = 2), ncol = 2))
nms <- names(attr(sp, "bb"))
attr(sp, "bb")[1, ] <- bb[c(2, 1, 4, 3)]
map_sp <- readOGR(dsn, "sp")
#> OGR data source with driver: ESRI Shapefile
#> Source: "/home/oswaldo/R/x86_64-pc-linux-gnu-library/3.4/ggsn/extdata", layer: "sp"
#> with 96 features
#> It has 2 fields
map_sp@data$id <- 0:(nrow(map_sp@data) - 1)
map_sp <- merge(tidy(map_sp), map_sp, by = 'id')
ggmap(sp) +
geom_polygon(data = map_sp, aes(long, lat, group = group, fill = nots),
alpha = .7) +
coord_equal() +
geom_path(data = map_sp, aes(long, lat, group = group)) +
blank() +
scalebar(map_sp, dist = 5, dd2km = T, model = 'WGS84') +
north(map)
Also, sp <- get_googlemap("São Paulo")
gives me a '400 Bad Request' error.
whereas sp <- get_googlemap("Sao Paulo")
returns the correct map.
Your example works great with when I use the my modified version of your north()
function, that replaced annotation_custom
with inset
.
What packages versions and operating system are you using?
I have no problems in Ubuntu with packages ggplot 2.2.1.9000, ggmap 2.6.1 and ggsn 0.4.2. If packages versions are not the problem, I will implement your solution.
R version 3.4.1 (2017-06-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
other attached packages:
[1] ggsn_0.4.0 rgeos_0.3-23 XML_3.98-1.9 gdalUtils_2.0.1.7 rgdal_1.2-8
[6] sp_1.2-5 ggmap_2.6.1 gridExtra_2.2.1 ggplot2_2.2.1
Not sure how you would check whether you are 'adding' the north()
to a ggmap or ggplot.
Please, try with ggsn 0.4.2.
library(devtools)
install_github("oswaldosantos/ggsn")
Your example works with ggsn 0.4.2. But I still get the same error with the following code.
library(ggmap); library(ggsn)
MapTiles <- get_map(
location = c(long = -122.407534, lat = 37.788834),
zoom = 18, source = "stamen", color = "bw", maptype = "toner-lite")
bb <- data.frame(
long = c(bb2bbox(attr(MapTiles, "bb"))[[1]],
bb2bbox(attr(MapTiles, "bb"))[[3]]),
lat = c(bb2bbox(attr(MapTiles, "bb"))[[2]],
bb2bbox(attr(MapTiles, "bb"))[[4]])
)
ggmap(MapTiles) +
north(data = bb, location = "topleft")
I have also tried adding north(MapTiles)
, but that gives Error: $ operator is invalid for atomic vectors
. Yet when I use your north()
function with inset
instead of annotation_custom
, it works.
I substituted inset
by annotation_custom
in ggsn 0.4.3.
@oswaldosantos I am getting the same error using 0.5.0 version. Is that fixed?
@oswaldosantos I'm getting this error with 0.5.0 as well.
Use north2 with ggmap: north2(ggmap(MapTiles))