I'm using R 3.1.2, trying to create a choropleth using a world map. The quirk is that, because my audience is in Asia I need to recenter the map. From the documentation it seems like nowrapRecenter() is perfect for this, but I find that it doesn't seem to work as advertised. For example, start without any recentering:
library(maps)
library(maptools)
library(rgdal)
data(wrld_simpl)
plot(wrld_simpl)
Now try to recenter at 148E longitude, in order to move Asia closer to the centre of the map while splitting as few land masses as possible at the left/right margins:
library(maps)
library(maptools)
library(rgdal)
data(wrld_simpl)
world <- nowrapRecenter(wrld_simpl,offset=148,avoidGEOS=TRUE)
plot(world)
What you get is a bit messy. Not only is the map centred at 180E longitude, but there are scratches all across the map where polygons which nowrapRecenter() should have been divided and re-closed at the left/right are extending across the full width of the map. In fact recentering does not appear to work cleanly for any chosen offset.
A similar question came up before, and the final comment there provided an example using nowrapRecenter(), but it no longer seems to work. What's the best way to recenter a world map (using SpatialPolygons) and have the polygons at the left/right margins be properly divided?
Thanks!
This is only a partial answer, so if it's not adequate let me know and I'll delete it.
The problem appears to be that transforming a Mercator projection fails near the poles. If you are willing to exclude Greenland and Antarctica, this works.
library(maptools)
data(wrld_simpl)
wrld <- wrld_simpl[!(wrld_simpl$NAME %in% c("Greenland","Antarctica")),]
library(ggplot2)
ggplot(wrld,aes(x=long,y=lat,group=group))+
geom_polygon(fill="white",color="grey30")+
coord_map(orientation=c(90,0,148))+
scale_x_continuous(breaks=c(0,60,120,180,-120,-60))+
theme_bw()
Even with this restriction, nowrapRecenter(...) failed.
Related
I'm trying to plot some coordinates that stretches across Antarctica, and since I really don't care about the rest of the world, I'm trying to find a way to plot just the Antarctic continent with R (i.e. showing a view from the south pole). Problem is, nobody seems to care about Antarctica and most of the stuff I'm able to find about how to plot coordinates in R straight up cut away the continent because it gets warped in the Mercator projection...
and now I'm completely stuck. Any advices on how to approach the problem? Or helpful sources?
Have you tried using a UTM projection in conjunction with leaflet?
You need to transform to a polar stereographic projection system.
require(maptools)
shp = wrld_simpl[which(wrld_simpl$NAME=="Antarctica"),]
shp = spTransform(shp,"+init=EPSG:3031")
plot(shp)
Is there a way to import data regarding province boundaries?
I understand that finding longitudes and latitudes for the capital (Tehran) is fairly easy, however, borders of a province are out of my league.
Something like this interactive map is simple and informative.
https://simplemaps.com/country/ir
I've gone to this site and downloaded the file called "irn_adm_unhcr_20190514_SHP.zip".
After you save and unzip it somewhere on your computer, run the following code to produce the below map:
library(sf)
library(ggplot2)
iran <- st_read("irn_admbnda_adm1_unhcr_20190514.shp")
ggplot(iran) + geom_sf()
Also, in general, searching for "[geographic entity] geojson" usually returns something useful.
https://github.com/mohsentaleb/iran-administrative-boundaries/blob/master/provinces.geojson
I am trying to overlay data on top of a map of Canada, however I can't adjust the zoom as I would like. In the map, I want to be able to see the lines for each province anong with its name (so using map("world", "Canada") isn't desirable)
I have tried altering the zoom, but one is too zoomed out and the other is too zoomed in:
qmap(location = "Canada", zoom = 3)
qmap(location = "Canada", zoom = 4)
I have tried researching on how to crop the image but have been unsuccessful
Thank you!
You can approach this using either the maps and mapsdata packages or continue with ggmap. It just depends on how much detail you want.
library(raster)
library(maps)
library(mapdata)
canada<-getData("GADM",country="CAN",level=1)
plot(canada,xlim=c(-141,-53),ylim=c(40,85),col="gray80",border="gray40",axes=T,las=1)
invisible(text(getSpPPolygonsLabptSlots(canada),labels=as.character(substr(canada$HASC_1,4,5)),
cex=0.75, col="black",font=2))
Using ggmap, you can specify a bounding box when grabbing your spatial data. It still overplots some of the area you are interested in (i.e., plots most of the US). Therefore, reapplying the bounding box values to the ggmap function cuts down the viewable area.
library(ggmap)
lats<-c(40,85)
lons<-c(-141,-53)
bb<-make_bbox(lon=lons,lat=lats,f=0.05)
cda<-get_map(bb,zoom=3,maptype="terrain")
ggmap(cda)+xlim(lons)+ylim(lats)+theme_bw()+labs(x="Longitude",y="Latitude")
I have a very small SpatialLinesDataFrame which I need to plot with Leaflet in R.
Unfortunately, for some reason I'm not able to do so.
This is the data (it's 12KB).
I try to do:
library(leaflet)
load("mylines.Rdata")
leaflet() %>% addTiles() %>% addPolylines(data=mylines)
However the resulting map does not make sense, I can only see a line in the top of the screen and this is not what should be plotted.
This is the result:
Instead, If I do:
library(mapview)
mapview(mylines)
Result:
It works perfectly, despite mapview using leaflet underneath.
What am I doing wrong in the Leaflet syntax?
I am used to working with leaflet providing rasters, so I usually use the addRasterImage function, which needs data projected over leaflet's display projection (EPSG:3857). However, for polygons and lines, as #TimSalabim correctly pointed out, this is not the case.
So the solution in this case was to just not reproject the data to the leaflet projection beforehand, and provide it in lat-lon coordinates (EPSG:4326).
mapview worked since it does this automagically.
how to plot something like this:
see image here, I didn't have 10 reputation so I can not post image, so paste a image URL here.
http://www.flickr.com/photos/97751721#N07/9089566951
or
http://www.flickr.com/photos/97751721#N07/9091786734
right part of this map is a zoom in map with limitation, that is what I want
if it is use R code or other program language will be more great!
If your have everything as Spatial object you could simply plot with standard plot() and set lim to what ever you like.
require(maptools)
shape <- readShapePoints("shapefile.shp")
plot(shape, xlim=c(minXcoordinate, maxXcoordinate) ylim=c(minYcoordinate, maxYcoordinate))
It is a little unclear to me what exactly you want to do. Do you just want to make a map with a specific set of lon/lat boundaries? Do you need to plot data on top of it? Do you need to control the appearance to make it look like the example you give (with line boundaries & minimal geographic information)?
The ggmap package may get you started on this. The syntax goes like this:
require(ggmap)
# The location argument defines the center of the map
exampleMap <- get_map(location = c(lon = -95, lat = 30), zoom=5)
ggmap(exampleMap)
You can then add data to the map if you like (exactly how is left as an exercise to the motivated student!)