r: Displaying Eastern Russia and North America on one map rnaturalearth - r

I'm still learning how to use R. I'm trying to display points in North America and Eastern Russia using the package "rnaturalearth". I'd like to just show Russia and North America, rather than all of Europe. I've included a picture with the area of the world that I want in a box here. I get this error whenever I input positive and negative longitude values.
Error in st_normalize.sfc(x, c(x_range[1], y_range[1], x_range[2], y_range[2])) :
domain must have a positive range
I couldn't find anything online, although I'm sure it's probably pretty simple. Any help would be appreciated!
Here is the code for the general mapping:
world <- ne_countries(scale = "medium", returnclass = "sf")
ggplot(data = world)+
geom_sf()+
coord_sf(xlim = c(160, -60), ylim = c(20, 90), expand = FALSE)

I had almost the exact same challenge to try and solve and found your question while trying to figure it out. Here are the links which helped me get the output I wanted. I won't give an exact solution myself as I think these links already have the solution well laid out.
This question contains almost the exact process you are looking for:
https://gis.stackexchange.com/questions/332106/union-anti-meridian-multipolygons-after-re-centering-world-map
This is what the individual in the above question used as a reference to edit the code from (which also includes the packages needed to do this):
https://github.com/valentinitnelav/valentinitnelav.github.io/blob/master/gallery/Shift%20central%20(prime)%20meridian.R#L73
Once you have the polygons shifted you can use "coord_sf()" as you tried before to get the subset of the map you want.
The only issue I have with this specific solution is that it will shift the longitude values on the map. This means they do not accurately represent the actual longitude values. So make sure to take that into account if you want to show the longitude values on the map.
I hope this helps you get the map you want!
Edit: I just remembered this site has a different way to get a similar solution under "Example of a failed transformation", depending on the map projection you want.
https://mgimond.github.io/Spatial/coordinate-systems-in-r.html

Related

Mapping Antarctica in R

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)

R : How to Plot a Zoomed in Map of a City

Is there a way to (or what is the best way to) plot a map of a specific city, along with roads or neighborhood outlines?
I am pretty new to using R, so I've done a bit of Googling for an answer, but the ones I come up with don't work too well:
With ggmap, it appears I'd have to pay to get a good map. This is probably fine for me, but I'd be curious to see if there's a free way, such as with the maps package. I tried mapping a city (Minneapolis) with the maps package, but it came back with an error which I can't figure out how to get rid of, either through experimenting or Googling for help. Below is the code I did:
Minnesota <- subset(states, region %in% c("minnesota"))
which just kept coming back with Error: object 'states' not found. This was my first attempt to bring up a map of which I could zoom in on using the maps package. Does this mean maps didn't install correctly? Or is there something I'm missing?
I also tried using map, which I believe is part of ggplot2, with the code below:
map(database = "state", xlim = c(-93.0, -94.0), ylim = c(44.5, 45.5))
Again, this was to try and get a zoomed in map of a particular city, Minneapolis, but I kept getting the error "all data out of bounds". I assumed this was because the coordinates I entered were incorrect, but they line up with the coordinates of the city. What does it mean that all data is out of bounds?
The last thing I tried was using the package rworldmap. With the code below:
minneapolis <- getMap(resolution = "low")
plot(minneapolis, xlim = c(-93, -94), ylim = c(44.5, 45.5))
I think this one was actually working, the problem is that it's so zoomed in that I can't see the borders that come with the package/plot. Would there be a way to add roads/neighborhood borders to a map like this one? It seems like that would be the best way for me to build a map for free, but maybe I'm wrong.
Thank you for any help and clarification.
If you want the map to be interactive, then you should consider using leaflet, You can check this tutorial.
If you want it to be static ggmap or tmap are good options.
You do no need to pay. You can access the goole api or the OSM api and get the data.

Create land surface temperature map in R

I have a csv file containing latitude, longitude and temperature for san joaquin valley. I want to plot the latitude and longitude on the y and x axes respectively.I want to use the google base map. The temperature at a particular latitude and longitude should be plotted as a surface map like shown in the image below.
Please guide me to achieve the output as shown in the picture.
http://i.stack.imgur.com/r8loG.png
Thank you so much.
You can very easily use ggvis to make the type of map that you want. You will need more than just lat and lon to make it happen if you are looking ot have some kind of color transition based on a prevalence of some kind. It may be in another column and tied to your labeling method.
Here is a solid site to help you get started.
ggvis mapping
Work through the examples to figure out what is happening beneath the code before you dive right into your own unless you are very familiar with R and the ggvis environment.
Without a starting code base, no one is going to be able to help you more than this...get started and then ask more specific questions.

Plotting a Map within a Map in R

I'm brand new to any programming and I'm starting right in the deep end with R!
So, I'm trying to plot a map of some dive sites from which me and a buddy collected some data, which are off the west coast of the Isla de Juventud. I have plotted the map of Cuba and a more zoomed in one of the Island in R, and what I'm hoping to achieve is something along the lines of the following, but I just chucked this together with paint so obviously it would be better quality produced in R.
https://www.dropbox.com/sh/xve973hvih1vw77/OZ-gGpDgsJ <- this link
The code I have for the cuba map and the island map is this, respectively.
map("worldHires", xlim=c(-85.3198254, -73.718262), ylim=c(190663280, 23.5), fill=TRUE, col="darkseagreen3", bgcol="grey80") map.axes()
and
map("worldHires", xlim=c(-83.25, -82.5), ylim=c(21.4, 22), fill=TRUE, col="darkseagreen3", bg="gray80") rect(-83.2, 21.5, -83.15, 21.65, border="red")
I'd rather learn how to do it or where I'm going wrong, if anywhere, here as I really want to improve my skills with this program.
I think you are seeing this the wrong way.
I read the manual for R package Maps in (http://cran.r-project.org/web/packages/maps/maps.pdf), and I couldn't find a map database called "worldHires"
To have a better understanding, try the commands
map('world2')
map.axes()
This should give a plot of the world map; however, to highlight certain cities, read about map.cities command in the package manual.
To get a map of Cuba for example try this
map("world2", region='Cuba',fill=TRUE, col="darkseagreen3")
map.axes()
I hope this helps
Have you loaded maps, mapdata, and maptools? That fixed the same problem for me!

how to plot a part of map but with limitation arround it

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!)

Resources