Plotting a Map within a Map in R - 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!

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: Displaying Eastern Russia and North America on one map rnaturalearth

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

Is there a way for plotting boundaries of Iran provinces in R?

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

How to plot map and data with respect to zipcodes/neighbourhoods for amsterdam

As there is a library dedicated to US zipcodes called as library(choroplethrZip) is there any other library for plotting zipcodes in Amsterdam?
I also tried using hcmaps to plot the neighbourhoods but I was not able to find deep.
mapdata <- get_data_from_map(download_map_data("countries/nl/nl-nh-all"))
I can only go till north holland but not deep into Amsterdam.
Can anyone help me with this?
Thanks in advance.
You can find the whole Highcharts map collection here: https://code.highcharts.com/mapdata/
Probably there is no such deep map displaying zipcodes in Amsterdam, so you should create a custom one which is not easy (especially integrating it in R, I have never seen this before). I hope this answer clarifies you a little.
https://www.highcharts.com/docs/maps/create-custom-maps-for-highmaps
https://www.highcharts.com/docs/maps/custom-geojson-maps

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.

Resources