"Regions defined for each Polygons" when mapping shp with R - r

I'm simply trying to visualize some land polygons from natural earth
landData2<-readOGR("/home/pavel/Documents/Studium/Hiwi/Maps/ne_10m_land", "ne_10m_land")
landGG2<- fortify(landData2)
after the second line I get the message
"Regions defined for each Polygons"
I already fortified it so this post was of no use
Why does ggplot give a blank page with the message "Regions defined for each Polygons"?
I got the data from
http://www.naturalearthdata.com/downloads/10m-physical-vectors/
I used the simple (not scale ranked) polygons
Does anyone know what it means?

Ok, this went fast.
Following this Post
Filling polygons of a map using GGplot in R
the problem seems to be that fortify does not transfer the id to a numeric variable
simply adding
landGG$id <-as.numeric(landGG$id)
helps
Still not sure why exactly this message comes out and why it is only true for the parts of the data frame after the "hole" variable (id, piece and group variable) but probably some problem with fortify
But it works.
Thought I'd leave it here instead of deleting

Related

plotting polygons inside loop in R, why this ambiguous behavior?

So, I want to plot multiple polygons over one polygon using a loop in r. I have read that you need to use print in order to succesively display the polygons over the original polygon. However, that is not what R seems to do. Besides, in a hardly reproducible example, what i find is that either using print or not, the polygons will not plot within the loop. I am using a mac with OS monterrey, btw.
Here is a reproducible example, using print gives a bunch of NULL messages, but does print, not using print still plots. Yet a more complicated example will never plot from within the loop despite plotting everything when i request line by line from within the loop.
require(maptools)
data(wrld_simpl)
plot(wrld_simpl)
for (i in 1:12){
region=wrld_simpl[wrld_simpl$NAME==wrld_simpl$NAME[i],]
print(plot(region,border="red",add=T))
}
Maybe you're confusing print and plot? The plot function creates (or adds elements to) a plot. Whereas print function prints its arguments to the console. Since plot doesn't return a value, print has nothing to print and hence prints NULL.
Btw. you don't even need a loop. Since the loaded data wrld_simpl is of class SpatialPolygonsDataFrame, the object is subsettable like a dataframe you can just plot the needed indices:
plot(wrld_simpl)
plot(wrld_simpl[1:12,], border="red", add=T)
If you need help on a more complicated example, I suggest you share it?

Mapping in R with maps package

I am currently trying to generate a map in R demonstrating where my different sampling locations are along the east coast of Canada. I had my code working previously yesterday and was able to visualize the specific area I wanted to look at, and then layer my point on top.
This morning however, when I open the project back up and try to generate the map with this line of code:
map("worldHires", "Canada", xlim=c(-80,-45), ylim=c(40,60), col="gray90", fill=TRUE)
I get the output [[1]] NULL. This is not trying to plot the sampling locations yet, just generating a blank map which I will plot the points over. All the necessary packages (to my knowledge) are loaded (maps, mapdata, maptools, scales) and I don't understand why it was working fine yesterday but today I am only getting this output. I have tried with other regions as well and no matter what I put into the argument it only returns NULL.
I have also tried just entering the line:
map("world", xlim=c(-80,-45), ylim=c(40,60), interior=TRUE)
However when I do this I get this error: Error in as_mapper(.f, ...) : argument ".f" is missing, with no default (which makes sense to me given that this line of code seems to be missing the argument of where it is drawing the geographical data from)
Any tips on what to do? I don't understand why it would be returning a NULL result and I haven't been able to find much help online so far.

What type of data do I need for maplines in R highcharter?

I'm trying to add a mapline object to a highcharter graph (ideally using hcmap as a base)--in my case, so county borders can look different than state borders.
I've seen the solution listed here, but it doesn't work for me--indeed, running the answer's example code fails to create different lines for states and counties. Meanwhile, the more complex code shown here does work, but I'm not sure what it's doing under the hood, or how I could use its method to create a mapline object of state boundaries. What kind of data do I need to add a working "mapline" series? Does anyone know a good source for one for state boundaries?
Any help would be appreciated!

Google Maps vs. ggplot2/MarMap

Below is a JavaScript page I have created that allows me add and freely move markers on the map. From this map I can figure out the regions I am interested in.
Basically what I want to do is show the same map using ggplot2/MarMap with coastline indicators + bathymetry data. I am really just interested in getting bathymetry data per GPS location, basically getting negative/positive elevation per Lat+Long, so I was thinking if I can plot it then I should be able to export data to a Database. I am also interested in coastline data, so I want to know how close I am (Lat/Long) to coastline, so with plot data I was also going to augment in DB.
Here is the R script that I am using:
library(marmap);
library(ggplot2);
a_lon1 = -79.89836596313478;
a_lon2 = -79.97179329675288;
a_lat1 = 32.76506070891712;
a_lat2 = 32.803624214389615;
dat <- getNOAA.bathy(a_lon1,a_lon2,a_lat1,a_lat2, keep=FALSE);
autoplot(dat, geom=c("r", "c"), colour="white", size=0.1) + scale_fill_etopo();
Here is the output of above R script:
Questions:
Why do both images not match?
In google-maps I am using zoom value 13. How does that translate in ggplot2/MarMap?
Is it possible to zoom in ggplot2/MarMap into a (Lat/Long)-(Lat/Long) region?
Is it possible to plot what I am asking for?
I don't know how you got this result. When I use your script, I get an error since the area your are trying to fetch from the ETOPO1 database using getNOAA.bathy() is too small. However, adding resolution=1 (this gives the highest possible resolution for the ETOPO1 database), here is what I get:
To answer your questions:
Why do both images not match?
Probably because getNOAA.bathy() returned an error and the object dat you're using has been created before, using another set of coordinates
In google-maps I am using zoom value 13. How does that translate in ggplot2/MarMap?
I have no clue!
Is it possible to zoom in ggplot2/MarMap into a (Lat/Long)-(Lat/Long) region?
I urge you to take a look at section 4 of the marmap-DataAnalysis vignette. This section is dedicated to working with big files. You will find there that you can zoom in any area of a bathy object by using (for instance) the subsetBathy() function that will allow you to click on a map to define the desired area
Is it possible to plot what I am asking for? Yes, but it would be much easier to use base graphics and not ggplot2. Once again, you should read the package vignettes.
Finally, regarding the coastline data, you can use the dist2isobath() function to compute the distance between any gps point and any isobath, including the coastline. Guess where you can learn more about this function and how to use it...

Spatial plot error after changes to dataframe

I was using the following code to plot a spatial plot successfully:
colours<-(brewer.pal(7, "Blues"))
brks<-classIntervals(EDdata2$SIRt, n=7, style="fixed",fixedBreaks=c(0,1,2,5.0,10.0,20,50,120))
plot(brks, pal=colours)
brks<-brks$brks
plot(EDdata2, col=colours[findInterval(EDdata2$SIRt, brks,
all.inside=TRUE)], axes=F, border=FALSE)
However I made some changes to the spatialpolygonsdataframe EDdata2 by adding some extra columns and changing how SIRt is calculated (however it remains a column of numbers - they are just calculate differently)
Now when I try to run the plot code I get an error despite having made no changes to the plotting code:
Error in plot.default(...) : formal argument "axes" matched by multiple actual arguments
Whats going on here ?
That means that the package author of the unnamed package you used to create EDdata2 defined a plot method for whatever class EDdata2 might be that both used the axes argument and also used the triple dot mechanism to pass arguments to plot.default without filtering out that argument. (This does suggest that the package author didn't really want you to make your own axes, so you should investigate the help page for plot.whatever to see if it offers a mechanism for passing the values you want to use for 'at' and 'labels'.) You will need to do the spadework yourself (or edit your answer to make it more complete and reproducible) to investigate.
If this code is using the plot method for the SpatialPolygons-class in package:sp then the default value for axes is already FALSE.
help("SpatialPolygons-class", package="sp")
It is of course possible to mess this up by defining "F" to be something other than "FALSE" and then using axes=F. In the current instance it might be simpler to just remove that argument from the call.

Resources