[R-sig-Geo] putting scalebar and north arrow on google map

Florian Detsch florian.detsch at staff.uni-marburg.de
Mon Jul 18 09:31:10 CEST 2016


Chris,

here is a rather manual lattice-based approach that might point you in 
the right direction. dismo::gmap(..., rgb = TRUE) returns a Raster* 
object that can easily be transformed to a list compatible with 
'sp.layout' by using mapview::rgb2spLayout(...). Note that if you want 
to stick with latlon coordinates (EPSG:4326), but display the scale bar 
in meters, you are required to specify dismo::gmap(..., lonlat = TRUE) 
and adjust the 'scale' argument used to create the scale bar accordingly.

Best,
Florian


library(dismo)
library(mapview)

## get map using `dismo::gmap`
OkatainaMap <- gmap(extent(176.425, 176.425, -38.0845, -38.0845),
                     type = 'hybrid', scale = 2, zoom = 15, rgb = TRUE)

## scale bar
scale <- list("SpatialPolygonsRescale", layout.scale.bar(), scale = 500,
               offset = c(19638250, -4592600), fill = c("white", "black"))
text1 <- list("sp.text", c(19638250, -4592670), "0", cex = .75, font = 2)
text2 <- list("sp.text", c(19638800, -4592670), "500 m", cex = .75, font 
= 2)

## north arrow
arrow <- list("SpatialPolygonsRescale", layout.north.arrow(type = 1),
               offset = c(19638450, -4592500), scale = 300)

## display data
spplot(OkatainaMap[[1]], colorkey = FALSE, col.regions = "transparent",
        scales = list(draw = TRUE),
        sp.layout = list(rgb2spLayout(OkatainaMap), arrow, scale, text1, 
text2))


On 18.07.2016 06:57, Chris Lusk wrote:
> Hi - I've made a map to show the spatial arrangement of my plots, and would
> now like to add a scale bar (in hundreds of metres) and a north arrow. I've
> seen several solutions online, most of which threw error messages when I
> tried them. Although the solution below doesn't give an error message, it
> doesn't actually add anything to the map!
>
> Any advice would be appreciated.
>
> Chris
> ..........
>
> library(ggplot2)
> library(ggmap)
> library(maps)
> library(GISTools)
>
> ##Coordinates of four types of plots
> Gaps <- read.table(text="lon lat
> 176.431823     -38.086894
> 176.4329002     -38.08855667
> 176.4286505     -38.08421246
> 176.4239767     -38.08297953
> 176.4306576     -38.09032491
> 176.4181457     -38.08283416
> 176.4325089     -38.09042394
> 176.4243027     -38.08215021
> 176.4220882     -38.08720573
> 176.430974    -38.093108", header = TRUE, strip.white = TRUE)
>
> Understorey <- read.table(text="lon lat
> 176.431523   -38.086965
> 176.418408   -38.082935
> 176.4244189     -38.08242069
> 176.4287644     -38.08372266
> 176.4240118     -38.0823004
> 176.4333778     -38.08853369
> 176.4217424     -38.0876122
> 176.4309304     -38.09055117
> 176.4324084     -38.0907023
> 176.430821   -38.093687", header = TRUE, strip.white = TRUE)
>
> Treeferns <- read.table(text="lon lat
> 176.432134      -38.086298
> 176.4246547     -38.08226916
> 176.4335132     -38.08801622
> 176.4183628     -38.08260261
> 176.429       -38.08413018
> 176.4213997     -38.08710868
> 176.4304333     -38.09064678
> 176.4324081     -38.0909994
> 176.4242689     -38.08264669
> 176.429837      -38.093061", header = TRUE, strip.white = TRUE)
>
> Margins <- read.table(text="lon lat
> 176.4225302     -38.08177849
> 176.427018     -38.08879161
> 176.4207786     -38.08356826
> 176.4315118     -38.08251629
> 176.426456     -38.08920441
> 176.426010   -38.088253
> 176.422526   -38.082794
> 176.421399   -38.084780
> 176.429339   -38.092742", header = TRUE, strip.white = TRUE)
>
> ## Get the map at right zoom level
> OkatainaMap <- get_googlemap(center = c(lon = 176.425, lat = -38.0845),
> maptype='hybrid', zoom=15)
>
> ## Plot the points on the map
> ggmap(OkatainaMap) +
>    geom_point(data=Gaps, aes(x=lon, y=lat), pch=15, colour="chartreuse1",
> size=2.5, alpha=1) +
>    geom_point(data=Understorey, aes(x=lon, y=lat), pch=21, colour="gray40",
> bg="black", size=3, alpha=1) +
>    geom_point(data=Treeferns, aes(x=lon, y=lat), pch=23, colour="gray40",
> bg="blue", size=2.5, alpha=1) +
>    geom_point(data=Margins, aes(x=lon, y=lat), pch=17, colour="orange",
> size=2.5, alpha=1)
>
> ##Add scale and north arrow (NEITHER COMMAND WORKS FOR ME)
>
> map.scale(x=176.42,y=-38.092, ratio=FALSE, relwidth=0.2, col="white")
> north.arrow(xb=176.435, yb=-38.079, len=0.02, lab="N", col="white")
>
> ...................
>
>
> Dr. Chris Lusk
> Senior Research Fellow
> Environmental Research Institute
> The University of Waikato
> Private Bag 3105, Hamilton
> New Zealand / Aotearoa
> http://sci.waikato.ac.nz/sites/clusk/
> Ph 64 7 838 4205
> Senior Editor, *NZ J Botany*
> ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
Florian Detsch (M.Sc. Physical Geography)
Environmental Informatics
Department of Geography
Philipps-Universität Marburg
Deutschhausstraße 12
35032 (parcel post: 35037) Marburg, Germany

Phone: +49 (0) 6421 28-25323
Web: http://umweltinformatik-marburg.de/en/staff/florian-detsch/



More information about the R-sig-Geo mailing list