[R-sig-Geo] Google maps/ Blue Marble in R
Oscar Perpiñán Lamigueiro
oscar.perpinan at gmail.com
Tue Sep 10 15:25:05 CEST 2013
Hello,
The next code combines three different objects using spplot. I have not
found the NBcontour10 data.frame in your last email. Therefore, I have
defined some points instead.
Best,
Oscar.
##################################################################
library(sp)
library(ggmap)
##You could also try library(OpenStreetMaps)
## Read limit data and define an SpatialLines object
limit <- SpatialLines(list(Lines(Line(limit_NBver10[,-1]), ID='1')),
proj4string=CRS("+proj=longlat +datum=WGS84"))
## A component of sp.layout in the final spplot call
sp.limit <- list('sp.lines', limit, lwd=.5, col='white')
## Choose some points inside your region and define an
## SpatialPointsDataFrame object with some noise.
## Replace this chunk code with the definition of your data
bbLimit <- bbox(limit)
nPoints <- 20
coords <- cbind(sample(seq(bbLimit[1,1], bbLimit[1,2], .1), nPoints),
sample(seq(bbLimit[2,1], bbLimit[2,2], .1), nPoints))
myPoints <- SpatialPointsDataFrame(coords,
data=data.frame(rnorm(nPoints)),
proj=CRS("+proj=longlat +datum=WGS84"))
## Retrieve tiles from Google Maps
map4ao <- get_map(location = c(lon = -30, lat = 15),
maptype = "satellite", scale=2, zoom=4)
## Read the attributes to pass them to grid.raster
bbMap <- attr(map4ao, 'bb')
latCenter <- with(bbMap, ll.lat + ur.lat)/2
lonCenter <- with(bbMap, ll.lon + ur.lon)/2
height <- with(bbMap, ur.lat - ll.lat)
width <- with(bbMap, ur.lon - ll.lon)
## Another component of the sp.layout in spplot
sp.raster <- list('grid.raster', map4ao,
x=lonCenter, y=latCenter,
width=width, height=height,
default.units='native')
## Finally, the spplot call: the main object to be displayed is the
## SpatialPointsDataFrame. The region limits and the map layers are
## printed with sp.layout.
spplot(myPoints,
scales=list(draw=TRUE), key.space='right',
sp.layout=list(sp.raster, sp.limit))
##################################################################
zuzana zajkova writes:
> Hi Oscar and Ben,
>
> thank you, I tried get_map & ggmap from {ggmpap}, it works quite good, I
> got the axes with lat/long what I wanted. The problem is that when I want
> to save the map bigger (width=2000), the axes get too small and are almost
> invisible... and I am not very familiar with ggmap to change it somehow (so
> far... :)
>
> Oscar, I like a lot the map in your post (
> http://procomun.wordpress.com/2013/04/24/stamen-maps-with-spplot/), the
> axes are as I would like to have them. Unfortunately, I was not able to
> addapt your code to my needs. Could you help me with it, please?
> I would like to plot a map using sattelite Google map as a base, in big
> resolution (scale =2). And on that plot few kernel contours and their
> centroides.
> (By the way, don't you know if it is possible to fill the contours of
> kernels?)
>
> I woul like to get an area aprox. from longitude -50 to -10 and latitude
> from -10 til 40.
> I attach data for a kernel and its centroide.
>
> ## EXAMPLE
> # this data are as dput at the end
> limit_NBver10<-read.table( "limit_NBver10.txt", header=T, sep="\t")
>
> # calculate centroide
> NBcentr10<- c(mean(NBcontour10$Long), mean(NBcontour10$Lat))
> NBcentr10m<- Mercator(NBcentr10)
>
> # plot Google map
> e <- extent( -60 , -10 , 0 , 40 ) ## extent (long, long, lat, lat)
> r <- gmap(e, type="satellite", scale=2)
> plot(r, interpolate=TRUE)
>
> ### I would prefer to use this or the spplot...
> # map4ao<-get_map(location = c(lon = -30, lat = 15), maptype = "satellite",
> scale=2, zoom=4)
> # ggmap(map4ao)
>
> # convert to Mercator to be able to plot it on Google Maps
> NB10<- cbind(limit_NBver10$Long, limit_NBver10$Lat)
> NB10m<- Mercator(NB10)
> lines(NBm, lwd=2, col="brown")
> points(NBcentr10m, pch=18, col="brown")
>
> I want to plot two different kernels (50 and 95) for two different
> localities, so adding a legend might be useful aswell.
>
> I would appreciate any hints, thank you!
>
> Zuzana
>
>> dput(limit_NBver10)structure(list(ID = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), Long = c(-35.76132,
> -35.764120449255, -35.768824388905, -35.7719686454691, -35.7757680708915,
> -35.7785437803288, -35.7805257086988, -35.7824125195278, -35.7837508021974,
> -35.7840633705864, -35.7833608340727, -35.7820936177787, -35.7800250824356,
> -35.7775624167126, -35.7744526624865, -35.7709437423894, -35.7665367970046,
> -35.7616226656884, -35.76132, -35.7568039536541, -35.7488041248386,
> -35.7417367184295, -35.7334540742505, -35.73168, -35.7256451878587,
> -35.7143121934187, -35.7041841252115, -35.70204, -35.6927510753647,
> -35.6804396152642, -35.6724, -35.6677073422709, -35.6523052179148,
> -35.64276, -35.6376241974088, -35.6195621922728, -35.61312, -35.6017260802916,
> -35.58348, -35.5826670599887, -35.5620999843372, -35.55384, -35.5389295835598,
> -35.5242, -35.5170406473499, -35.49456, -35.4910968267571, -35.46492,
> -35.4631971048108, -35.43528, -35.4336153397102, -35.40564, -35.4024270676739,
> -35.376, -35.3683641663942, -35.34636, -35.3304392819369, -35.31672,
> -35.2889372897805, -35.28708, -35.25744, -35.2456741928817, -35.2278,
> -35.19816, -35.1944915971781, -35.16852, -35.13888, -35.1385549593096,
> -35.10924, -35.0796, -35.0747904983599, -35.04996, -35.02032,
> -34.9994625370281, -34.99068, -34.96104, -34.9314, -34.9025164121559,
> -34.90176, -34.87212, -34.84248, -34.81284, -34.7832, -34.7630178746876,
> -34.75356, -34.72392, -34.69428, -34.66464, -34.635, -34.60536,
> -34.57572, -34.54608, -34.51644, -34.4868, -34.45716, -34.42752,
> -34.39788, -34.36824, -34.3386, -34.3229081590724, -34.30896,
> -34.27932, -34.24968, -34.22004, -34.1904, -34.1833398204767,
> -34.16076, -34.13112, -34.10148, -34.0840608741637, -34.07184,
> -34.0422, -34.01256, -34.0062808859247, -33.98292, -33.95328,
> -33.9385102787257, -33.92364, -33.894, -33.880277734286, -33.86436,
> -33.83472, -33.827347612401, -33.80508, -33.7796594326867, -33.77544,
> -33.7458, -33.7361891314615, -33.71616, -33.694484349375, -33.68652,
> -33.6575769642992, -33.65688, -33.62724, -33.6221516529997, -33.5976,
> -33.5892583802976, -33.56796, -33.5578165415032, -33.53832, -33.528820118836,
> -33.50868, -33.5015590210146, -33.47904, -33.4751799038959, -33.4504413091046,
> -33.4494, -33.426306458131, -33.41976, -33.4054056142496, -33.39012,
> -33.3844045258165, -33.3635961017649, -33.36048, -33.3455473492392,
> -33.33084, -33.3271396204109, -33.3106618988982, -33.3012, -33.2941852476727,
> -33.2781605228234, -33.27156, -33.264705819104, -33.2513991432137,
> -33.24192, -33.238800798226, -33.227270068641, -33.215349396401,
> -33.21228, -33.2051919732298, -33.1959278297877, -33.1860106844991,
> -33.18264, -33.1784650580848, -33.1715035446386, -33.1640273139746,
> -33.1568538785492, -33.153, -33.1521877863785, -33.147020318298,
> -33.1432993363034, -33.139559306962, -33.1366906641853, -33.1341692689418,
> -33.1323814515189, -33.1311608943111, -33.1306733938088, -33.1314681316263,
> -33.1325819830105, -33.1346501704358, -33.13721035272, -33.1400704025971,
> -33.1439444924953, -33.1484296611689, -33.153, -33.1532499229485,
> -33.1580637496955, -33.1662932362206, -33.173646667577, -33.1816649203705,
> -33.18264, -33.1899750149979, -33.2009147351574, -33.2115073405822,
> -33.21228, -33.2233892824875, -33.2356153568674, -33.24192, -33.2480137447903,
> -33.2630863399439, -33.27156, -33.2780095781416, -33.295829022396,
> -33.3012, -33.313455253761, -33.33084, -33.3319832022492, -33.3518240927414,
> -33.36048, -33.374091292595, -33.39012, -33.3956290868364, -33.41976,
> -33.4204910766419, -33.4468518038561, -33.4494, -33.4742242915532,
> -33.47904, -33.5040200529036, -33.50868, -33.5364525254934, -33.53832,
> -33.56796, -33.5704387195682, -33.5976, -33.6061915213228, -33.62724,
> -33.6473983972767, -33.65688, -33.68652, -33.6901652456419, -33.71616,
> -33.7380282545386, -33.7458, -33.77544, -33.7908991328054, -33.80508,
> -33.83472, -33.8511831363073, -33.86436, -33.894, -33.9219966999333,
> -33.92364, -33.95328, -33.98292, -34.0035253731934, -34.01256,
> -34.0422, -34.07184, -34.10148, -34.1136530559176, -34.13112,
> -34.16076, -34.1904, -34.22004, -34.24968, -34.27932, -34.30896,
> -34.3316347601224, -34.3386, -34.36824, -34.39788, -34.42752,
> -34.45716, -34.4689166977478, -34.4868, -34.51644, -34.54608,
> -34.57572, -34.60536, -34.635, -34.66464, -34.6875167969269,
> -34.69428, -34.72392, -34.75356, -34.7832, -34.7992810632476,
> -34.81284, -34.84248, -34.87212, -34.882727179189, -34.90176,
> -34.9314, -34.9523285254729, -34.96104, -34.99068, -35.0123641694232,
> -35.02032, -35.04996, -35.0661456656777, -35.0796, -35.10924,
> -35.1131902567721, -35.13888, -35.1584008642754, -35.16852, -35.1981171043314,
> -35.19816, -35.2278, -35.2360511593816, -35.25744, -35.2714490107246,
> -35.28708, -35.3045772317784, -35.31672, -35.3367740716699, -35.34636,
> -35.3657477183956, -35.376, -35.3927354154775, -35.40564, -35.4190313708612,
> -35.43528, -35.4444541324849, -35.46492, -35.4676591581021, -35.4906713829709,
> -35.49456, -35.5107391912776, -35.5242, -35.5315056927973, -35.5510690641519,
> -35.55384, -35.5682272848858, -35.58348, -35.5857470934045, -35.6017746761061,
> -35.61312, -35.6177419467398, -35.6320260505406, -35.64276, -35.6467320400089,
> -35.6593093967563, -35.6720315641026, -35.6724, -35.683151614379,
> -35.6956377030949, -35.70204, -35.7050075805178, -35.7140655762887,
> -35.7240504442932, -35.73168, -35.7317594920335, -35.7397005637197,
> -35.7465500670664, -35.7532583327874, -35.759236627859, -35.76132
> ), Lat = c(8.08043387144557, 8.09768, 8.12732, 8.15696, 8.1866,
> 8.21624, 8.24588, 8.27552, 8.30516, 8.3348, 8.36444, 8.39408,
> 8.42372, 8.45336, 8.483, 8.51264, 8.54228, 8.57192, 8.57377979511483,
> 8.60156, 8.6312, 8.66084, 8.69048, 8.69735379500348, 8.72012,
> 8.74976, 8.7794, 8.78519741826762, 8.80904, 8.83868, 8.85773510801789,
> 8.86832, 8.89796, 8.91775185850685, 8.9276, 8.95724, 8.96823259299761,
> 8.98688, 9.01523738440848, 9.01652, 9.04616, 9.05726692469371,
> 9.0758, 9.09632840474305, 9.10544, 9.13106663320714, 9.13508,
> 9.16286208184106, 9.16472, 9.19278220396127, 9.19436, 9.2211597736667,
> 9.224, 9.24737294443929, 9.25364, 9.27039378128219, 9.28328,
> 9.29349850391879, 9.31292, 9.31419728857203, 9.33509437807802,
> 9.34256, 9.35276282893715, 9.37038298084766, 9.3722, 9.38581027591707,
> 9.40169210436488, 9.40184, 9.41549137459375, 9.4296117151511,
> 9.43148, 9.4411204879236, 9.45393231831201, 9.46112, 9.46393441172051,
> 9.47303685870308, 9.48325580198858, 9.49076, 9.49095227452636,
> 9.49871209676193, 9.50508729376379, 9.51180566511919, 9.51749291069681,
> 9.5204, 9.52175454666723, 9.52613292127168, 9.52931720127422,
> 9.53185261838295, 9.53438273000332, 9.53590291885375, 9.53705028505951,
> 9.53748476039723, 9.53715498606973, 9.53621258767864, 9.53470379780641,
> 9.53229443830216, 9.5297340327994, 9.52697876696969, 9.52274088148886,
> 9.5204, 9.5182322231653, 9.5131363094554, 9.50621649988915, 9.49963575167232,
> 9.49233807626853, 9.49076, 9.48580741920927, 9.47572164021867,
> 9.46635011366293, 9.46112, 9.45741198601691, 9.44524008421767,
> 9.43414609610616, 9.43148, 9.42126011475836, 9.40821089262206,
> 9.40184, 9.39488719114209, 9.37932469183404, 9.3722, 9.36347199806197,
> 9.34687475914923, 9.34256, 9.32858750627055, 9.31292, 9.31039774939372,
> 9.28982378339654, 9.28328, 9.26864288757179, 9.25364, 9.2479080674308,
> 9.224, 9.22346871038981, 9.19873873385822, 9.19436, 9.17238054127573,
> 9.16472, 9.14489695991995, 9.13508, 9.11539558249129, 9.10544,
> 9.08359258376715, 9.0758, 9.05064566217321, 9.04616, 9.01652,
> 9.01515032231369, 8.98688, 8.97789845710278, 8.95724, 8.93601062515183,
> 8.9276, 8.89796, 8.89271179631726, 8.86832, 8.84488136910915,
> 8.83868, 8.80904, 8.79244529979455, 8.7794, 8.74976, 8.73576197549069,
> 8.72012, 8.69048, 8.66832487838227, 8.66084, 8.6312, 8.60156,
> 8.59284714772742, 8.57192, 8.54228, 8.51264, 8.49960393982654,
> 8.483, 8.45336, 8.42372, 8.39408, 8.36966939063064, 8.36444,
> 8.3348, 8.30516, 8.27552, 8.24588, 8.21624, 8.1866, 8.15696,
> 8.12732, 8.09768, 8.06804, 8.0384, 8.00876, 7.97912, 7.94948,
> 7.91984, 7.89167518757741, 7.8902, 7.86056, 7.83092, 7.80128,
> 7.77164, 7.7680311659886, 7.742, 7.71236, 7.68272, 7.68073415574699,
> 7.65308, 7.62344, 7.60782464966612, 7.5938, 7.56416, 7.54675806399753,
> 7.53452, 7.50488, 7.49553887370153, 7.47524, 7.44749926243359,
> 7.4456, 7.41596, 7.40409807229935, 7.38632, 7.36379203901634,
> 7.35668, 7.32796296508197, 7.32704, 7.2974, 7.29457983831339,
> 7.26776, 7.26284720560734, 7.23812, 7.23368581395931, 7.20848,
> 7.20680873133046, 7.18078990873599, 7.17884, 7.15562923958771,
> 7.1492, 7.13466995868126, 7.11956, 7.11287698290142, 7.09209875797376,
> 7.08992, 7.07421759573464, 7.06028, 7.0556926420228, 7.03895935912814,
> 7.03064, 7.02365199428843, 7.00799627559596, 7.001, 6.99571065155614,
> 6.98289529233856, 6.97136, 6.97070164584002, 6.9598352191266,
> 6.94788931512401, 6.94172, 6.93913351612368, 6.9309494477673,
> 6.92281419400638, 6.91474401489769, 6.91208, 6.90841695153588,
> 6.90314181581088, 6.8978612966742, 6.89328053024696, 6.88837951169988,
> 6.88558334082918, 6.88377514061031, 6.88244, 6.88204785172503,
> 6.88128125292644, 6.88064811689433, 6.88095820050657, 6.88192925023196,
> 6.88244, 6.88322681012764, 6.88514338870819, 6.88714488113039,
> 6.89211296274698, 6.89666287906126, 6.90195489326937, 6.90727875711154,
> 6.91208, 6.91354044611272, 6.92074136095577, 6.9290394598828,
> 6.93715832073218, 6.94172, 6.94556457131539, 6.95722489866731,
> 6.96746773905942, 6.97136, 6.978870137199, 6.9920276332904, 7.001,
> 7.00485446418268, 7.02029966682933, 7.03064, 7.03448426462747,
> 7.05153942092025, 7.06028, 7.06839180015739, 7.08736078281851,
> 7.08992, 7.10718007994466, 7.11956, 7.12644127848716, 7.1492,
> 7.14922989749416, 7.1723016416664, 7.17884, 7.19639511559498,
> 7.20848, 7.22274226335147, 7.23812, 7.24910681944113, 7.26776,
> 7.27719345688089, 7.2974, 7.30831876417049, 7.32704, 7.34148284282932,
> 7.35668, 7.37502997050363, 7.38632, 7.41237069298967, 7.41596,
> 7.4456, 7.45119407599648, 7.47524, 7.49421100853822, 7.50488,
> 7.53452, 7.53936477586162, 7.56416, 7.58978285878249, 7.5938,
> 7.62344, 7.64425216040911, 7.65308, 7.68272, 7.70396998505454,
> 7.71236, 7.742, 7.77164, 7.77259003951664, 7.80128, 7.83092,
> 7.85098591317257, 7.86056, 7.8902, 7.91984, 7.94917139701681,
> 7.94948, 7.97912, 8.00876, 8.0384, 8.06804, 8.08043387144557)), .Names
> = c("ID",
> "Long", "Lat"), class = "data.frame", row.names = c(NA, -359L
> ))
>
>
>
>
>
>
> On 6 September 2013 08:58, Oscar Perpiñán Lamigueiro <
> oscar.perpinan at gmail.com> wrote:
>
>> Hello,
>>
>> I am not sure if it fits your problem, but perhaps this example I posted
>> some months ago is useful for you:
>> http://procomun.wordpress.com/2013/04/24/stamen-maps-with-spplot/.
>> It combines a Stamen map (retrieved with the ggmap or OpenStreetMap
>> packages) with an SpatialPointsDataFrame displayed with spplot.
>>
>> Could you send a sample of your data to give you a better answer?
>>
>> Best,
>>
>> Oscar.
>>
>> --
>> Oscar Perpiñán Lamigueiro
>> Grupo de Sistemas Fotovoltaicos (IES-UPM)
>> Dpto. Ingeniería Eléctrica (EUITI-UPM)
>> URL: http://http://oscarperpinan.github.io
>> Twitter: @oscarperpinan
>>
>> zuzana zajkova writes:
>>
>> > Hi,
>> >
>> > I would like to ask some questions... My aim is produce nice maps of
>> > distribution (kernels) of my studied species. So far I have been using
>> > "rworldmap" to get general view, but for presentation I would like to
>> make
>> > the maps more beautiful :)
>> > I was able to get the Google maps (using "dismo" package) and create a
>> map,
>> > but I am not able to add axes and legend to this map, what makes it quite
>> > useless as a map... Any suggestions?
>> >
>> > library(dismo)
>> > e <- extent( -60 , -10 , 0 , 40 ) ## extent (long, long, lat, lat)
>> > r <- gmap(e, type="satellite", scale=2)
>> > plot(r, interpolate=TRUE)
>> >
>> >
>> > Other thing is that I am open to use any other map as a background
>> > (preferably with topografhy and bathymetry), for example does anyone know
>> > if it is possible to use "Blue Marble" map for this?
>> >
>> > Thank you in advance for your suggestions.
>> >
>> > Zuzana
>> >
>> > [[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
>>
>>
--
Oscar Perpiñán Lamigueiro
Grupo de Sistemas Fotovoltaicos (IES-UPM)
Dpto. Ingeniería Eléctrica (EUITI-UPM)
URL: http://http://oscarperpinan.github.io
Twitter: @oscarperpinan
More information about the R-sig-Geo
mailing list