I working with spatial data in R for a commercial application and would like to use ggplot2 for data visualization. If you run the Hadley's example at https://github.com/hadley/ggplot2/wiki/plotting-polygon-shapefiles you find that in order to run the fortify command you need to enable the use of gpclib tools using gpclibPermit().
I'm looking for an efficient way (that doesn't involve manually hacking into the S4 object) to perform the same operation that fortify does here, i.e. take a spatial polygon object and turn it into a regular data frame where row entries contain latitudinal and longitudinal coordinates along with a polygon id.
Has anyone else solved this one?
You need to also install the rgeos package. When maptools is loaded and rgeos is not installed, the following message is shown:
> require("maptools")
Loading required package: maptools
Checking rgeos availability: FALSE
Note: when rgeos is not available, polygon geometry
computations in maptools depend on gpclib,
which has a restricted licence. It is disabled by default;
to enable gpclib, type gpclibPermit()
When fortify is called with a region argument (as it is in the example you linked to), then some "polygon geometry computations" need to be done. If rgeos is not available, and gpclib is not permitted, it will fail.
Related
i'm trying to use the Kest function (and many other in the spatstat package).
i have made a ppp point pattern data set (ppp.1)
summary (ppp.1)
Planar point pattern: 189 points
Average intensity 241122300 points per square unit
Coordinates are given to 6 decimal places
Window: rectangle = [40.74603, 40.74662] x [-111.84693, -111.8456] units
(0.000592 x 0.001324 units)
Window area = 7.83834e-07 square units
when i try to use the Kest function: Kest(ppp.1), i get the following error:
Error in Kest(ppp.1) : could not find function "Kest"
in fact, there are many functions in the spatstat package that can't be found (e.g., rpoint)... i get the same error.
does this have anything to do with "spatstat.random" not being found when i load the spatstat library:
Error: package ‘spatstat.random’ required by ‘spatstat’ could not be found
i'm using the most current version of R and spatstat (on an intel Mac):
R version 4.2.1 (2022-06-23) -- "Funny-Looking Kid"
Copyright (C) 2022 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin17.0 (64-bit)
spatstat 2.3-4 downloaded installed directly from R.
thank you.
Some time ago spatstat was one big package containing all the functions you mention, but due to technical requirements from CRAN it has now been split into several smaller packages all named according to the scheme spatstat.xxxx such as spatstat.random. The package spatstat is now an umbrella package with barely any functions, but it depends on spatstat.random and others, and it loads all these packages when you execute library(spatstat) in the R console.
Under normal circumstances R should refuse to install spatstat without all the needed sub-packages, but it appears you have an installation of spatstat without e.g. spatstat.random. Probably the easiest solution is to remove spatstat and install it again:
remove.packages("spatstat")
install.packages("spatstat", dependencies = TRUE)
Alternatively you can try (requires a relatively new version of spatstat to already be installed):
pkgs <- spatstat::spatstat.family()
install.packages(pkgs)
To find information about a package, you can visit its CRAN page (go to cran.r-project.org and look for Contributed Packages).
The CRAN page for spatstat says that spatstat 2.3-4 requires the packages spatstat.random and spatstat.core. This means that spatstat depends on code that is provided in these other packages.
Your error message says that spatstat.random is missing. This explains why the random generation function rpoint was not found.
As #EgeRubak says, the best solution is to remove and re-install spatstat.
I've made a package that exports spatial polygons objects. When using such objects, R automatically loads the sp package. Due to lazy loading of data, I was hoping that as long as the user does not use these spatial polygons objects, the sp package would not be loaded yet.
What I want
That the sp package is not loaded until the user uses / loads one of the spatial polygons objects in my package.
What actually happens
When I attach my package with library(myPackage), it does indeed not load sp. However, all functions and objects in my package start with mir_, and as soon as I type mir_ into my console, R loads sp. (I don't even need to execute any code, or even select any function or object. Just typing mir_ is enough.)
My questions
Why does R load sp even though I haven't used a spatial polygons object?
Is it possible to solve this issue, so that sp is only loaded when a spatial polygons object is used / loaded?
Extra info
I'm using LazyData: true in my DESCRIPTION file.
Being concerned with a spatial data analysis with data ranging from 1950 to 2016 I would like to plot some nice features in a world map in R. However, as the political landscape has changed with time, the actual implementations (e.g. the R package rworldmap) give the recent countries (Although, not long ago that was not true for all packages: Updated world map for R "maps" package?).
Hence my question is, whether there is some package with historic maps or otherwise some online-source for shapefiles or polygons for worldmaps, beginning in 1950.
Try the package cshapes from CRAN:
R Package for CShapes, a GIS dataset of country boundaries
(1946-2015). Includes functions for data extraction and the
computation of weights matrices.
It lets you extract a dataset for a chosen time, which you can then also use with any mapping tool you prefer.
library(maps)
1> map.where(database="world",29.392089,53.592505)
[1] "USSR"
does anyone know how I can get an updated world map database to drive this function in the maps package? I need country names only for now, not detailed sub-national administrative information such as is available in at gadm.org.
Try wrld_simpl in the maptools package.
require(maptools)
data(wrld_simpl)
plot(wrld_simpl)
## or subset based on the name
plot(wrld_simpl[wrld_simpl$NAME == "Russia", ])
## explore other attributes
summary(wrld_simpl)
I don't know how up to date it is, but ?wrld_simpl describes the source so you might find good stuff following links. Other packages that rely on sp will also be worth exploring for data.
Otherwise, there was the Rgshhs package, though I'm not sure that's still available. It came with fair detail but you can download more if you need it. It's a bit complicated, the original data is here: http://www.ngdc.noaa.gov/mgg/shorelines/gshhs.html
You can also read in data from vector formats (like MIF or SHP or PostGIS) using rgdal if you have your own data, or similar read functions in maptools for shapefile only.
Example to query the objects using points:
require(sp)
require(maptools)
data(wrld_simpl)
pts <- SpatialPoints(cbind(c(29.392089,147), c(53.592505, -35)), CRS(proj4string(wrld_simpl)))
over(pts, wrld_simpl)$NAME
For an introduction to these and other functions in sp see vignette("sp").
Also try the geonames package for more general querying of geographic names.
Lately I have seen some cool examples of mapping in R and wanted to give this a shot. I currently have ArcView at work, but my spatial join is not working correctly (most likely user error).
Objective: I need a list of countries and what World Region they belong to. I have two layers (one country detail, the other region detail) and wanted to join the world region assignment onto each country. The join isn't working, so i figured I would come to the R community.
What are my options? This is my first attempt at doing any mapping in R and maybe there is an easier/better solution. Eventually I want to take lat/long data and map it as well.
Any insight will be much appreciated.
Brock
See the Spatial task view on CRAN, and packages like maps/mapdata, sp, rgdal, raster, blighty, rworldmap, RgoogleMaps, etc.
Do you have shapefiles you want to read? First get rgdal installed, or look at other options like maptools and shapefiles if that is difficult on your platform. Read functions in these packages will provide Spatial*DataFrame objects.
For information on the Spatial classes:
library(sp)
vignette("sp")
spatstat also has a lot of support for spatial data, and another vignette for converting to / from sp:
library(spatstat)
vignette("shapefiles")
The PBSmapping package is another good place to start. They have pretty extensive documentation and a great reference manual as well.