How can I see all argument options in R? - r

This seems like a stupid question, but I have not been able to find the answer by googling.
R Documentation often does not outline all possible values for particular arguments. Is there a command to print that information?
For example, I would like to draw some maps using the function map() in the maps package. The R Documentation states for the region argument:
regions: character vector that names the polygons to draw. Each
database is composed of a collection of polygons, and each polygon has
a unique name. When a region is composed of more than one polygon, the
individual polygons have the name of the region, followed by a colon
and a qualifier, as in michigan:north and michigan:south. Each element
of regions is matched against the polygon names in the database and,
according to exact, a subset is selected for drawing. The default
selects all polygons in the database.
Is it possible to get a list of the region names?

You can see the arguments and defaults with:
args(map)
I believe you are looking for elements in the database and not arguments since the map function will not know what database you are passing it. Its default is 'world'.
w <- map('world')
str(w) # not necessary, but good to look at
w$names
And further:
canada <- map(region = 'Canada')
canada$names

Related

Extract from OSM Planet File is incomplete

I'm fairly new to the OSM world and I need to extract all water related polygons from the OSM planet file, except for ocean polygons. I know there is some product from a university in Tokyo, but it's from 2016 and I need it as up to date as possible.
I already extracted a good bit of it with the following code. However, comparing the resulting layers with the OSM basemap in QGIS I noticed that some parts are missing. Even though they have the same flags and relations like other parts that were extracted. I know that some parts of rivers are digitalized as lines and not polygons, so it's okay that those are missing. The missing parts are definitely polygons, since I could extract one of them with the same flags through the QuickOSM plug-in in QGIS. Also the OSM Basemap shows clearly that those areas must be polygons (see screenshot).
Is there a mistake in my code or did I make an mistake with the flags? My code however throws no errors and everything seems to be working except for the missing parts.
Thanks in advance!
Here is the code so far:
library(gdalUtils)
library(rgdal)
library(sf)
# extracting all layers with flag "natural = water"
path_pbf <- "path/to/planet_file.osm.pbf"
ogr2ogr(src_datasource_name = path_pbf,
"OSM_Waterbodies.gpkg",
f = "GPKG",
sql = "SELECT * FROM multipolygons WHERE natural = 'water'",
progress = T)
# extracting all layers with flag "other_tags LIKE waterway"
ogr2ogr(src_datasource_name = path_pbf,
"OSM_Waterways.gpkg",
f = "GPKG",
sql = "SELECT * FROM multipolygons WHERE other_tags LIKE '%waterway%'",
progress = T)
waterways <- st_read("OSM_Waterways.gpkg")
waterways$rm <- NA
# select only certain polygons since waterways also includes dams etc
check <- "*riverbank*|*river*|*stream*|*tidal channel*|*canal*|*drain*|*ditch*|*lake*"
# mark polygons which are not part of the desired selection with "remove" flag
for(i in 1:nrow(waterways)){
if (!grepl(check, waterways$other_tags[i])){
waterways$rm[i] <- "Remove"
}
}
# drop rows with "remove" flag
index <- which(waterways$rm == "Remove")
waterways <- waterways[-index,]
st_write(waterways, "OSM_Waterways_clean.gpkg", driver = "GPKG")
P.S.: The code is probably not the most efficient one, but it's not about efficiency, since I will probably run it once or twice.
It looks like you're only extracting multiploygons, which are used in OSM when a shape isn't a simple polygon. This means that river sections with islands in them will be extracted, but many simple river sections will not, as they are just mapped as closed ways (An example from your screenshot). I don't have the OSM Global file on hand to check, but I would imagine that it's as simple as running the ogr2ogr functions again with ways instead of multipolygons in the SQL, and then checking that the ways are closed (likely, check that the first and last nodes are identical, as a quick search suggests that ogr2ogr doesn't provide a way to check for closed ways explicitly).

Extract values in a Spatial Polygon dataframe

I'm currently trying to find out a value of a certain polygon in my Spatial Polygon dataframe. I need to know what "ID" was assigned to each "FSA" level. Below is the picture of how my data is set up. The dataset is called "FSA2015EditedFINAL2". I tried this:
FSA2015EditedFINAL2#polygons[[1]]#ID
But that just tells me the first polygons ID, and I don't know what FSA that corresponds to...
I'm hoping to be able to type in the FSA I need and pull out what ID it got.
Any help would be amazing!!!!
Thanks!

Tableau Map Line Graph - change linking order of data points

I have data with lat/lon attributes that I'm plotting onto a map. I then use the "line" mark to link the data points. It seems to automatically order them by the latitude attribute, which happens to bring two of my data points out of order:
How can I manually change the order of the linking?
Thanks!
There is a path shelf in Tableau, details of the usage can be found here:
http://kb.tableau.com/articles/knowledgebase/using-path-shelf-pattern-analysis
You just need to create a dimension and give each coordinate of your path a running number. Once you drop that dimension on the path shelf, Tableau will use it to determine the order of the coordinates.

Displaying Points in Scatterplot

Currently trying to write a simple r script that when passed in 2 vectors of values would calculate some relationship between them (in the given case, r_square) and display it in a graph with the best fit line.
temp1 <- sample(20000,1367,replace=F,prob=NULL)
temp2 <- sample(20000,1367,replace=F,prob=NULL)
fit <- lm(temp1 ~temp2)
plot(temp1,temp2,ann="true")
abline(fit)
(here using sample in lack of real data).
The problem is that i'm trying to add interactivity which would display point's value (X/Y coordinates of sort) on hover.
I've managed to find a few functions that identify them by the their order in the vectors (HWidentify, identify, etc). But none of them give the actual value (x,y) so i was wondering if it's possible to print out coordinates that aren't permanent.
If you're trying to print the coordinates, you could use the labels argument to the identify() function.
identify(temp1, temp2, labels=paste(temp1, temp2, sep=","))
The HWidentify function also has a labels argument that you can set to whatever you want, using the paste function like #JeanV.Adams works similarly and then you have the hover functionability.

How to specify countries/region while creating maps in R?

map("usa") by default displays a map without Alaska and Hawaii. map("world") has Antartica by default. Is there any way to say "include alaska", "exclude antartica" etc?
Quick answer:
nams <- map("world", namesonly=TRUE, plot=FALSE)
map("world", region=nams[-grep("Antarctica", nams)])
Longer answer:
The maps data in "world" is referenced by region names and these are generally character data in "continent:country" or "continent:subregion" format. To get those names which are in an external database, you need to first use maps("world" , ...) with parameters that return only the names and not all of hte other coordinates. If you want to find all the "Antarctica" containing entries you need to use grep() to identify their position in the returned names vector.
If you looking just for those areas, the brutal solution would be to use world map, specify USA as a region and define latitude/longitude to create limits, so the map will only display specific area:
library(maps)
long <- c(-180,-50)
lat <- c(10,80)
map("world",regions=".*usa",xlim=long,ylim=lat)

Resources