# Questions tagged [r]

Statistical computing language and software environment.

3,210 questions
136k views

### How to open a shapefile in R?

I need to open a shapefile from ArcMap in R to use it for further geostatistical analysis. I've converted it into ASCII text file, but in R it is recognized as data.frame. Coordinates function doesn't ...
10k views

### What is the most useful spatial R trick?

R is becoming pretty strong tool for handling and analyzing spatial data. I learned some useful things through questions like these at SO and thought it might be useful to have something simmilar, but ...
32k views

### Tutorials to handle spatial data in R?

Does anyone know some good tutorials to handle spatial data in R? I know that there are some packages like rgdal and maptools but I haven't actually found usefull tutorials to get in touch with the ...
34k views

### Making beautiful maps in R? [closed]

There are quite some nice possibilities to analyse spatial data in R and in the context of my current project I would like to use R more frequently to do that. Until now I plot my maps with ggplot2 ...
36k views

### How can I convert data in the form of lat, lon, value into a raster file using R?

I have a data set of values over a km grid in the continental U.S. The columns are "latitude", "longitude", and "observation", e.g.: "lat" "lon" "yield" 25.567 -120.347 3.6 25.832 -120....
38k views

### How to calculate polygon centroids in R (for non-contiguous shapes)

I've spent a little while figuring out the answer to this question. It's not immediately obvious from a Google search, so thought it may useful to post the answer on here. There is also an additional ...
26k views

### Reading KML file into R?

I'm working with huge .kml files (up to 10 Gb) and need an efficient way to read them into R. Until now I've been converting them to shapefiles via QGIS and then back into R with readShapePoly and ...
20k views

### How best to fix a non-noded intersection problem in PostGIS?

I'm using a PL/R function and PostGIS to generate voronoi polygons around a set of points. The function that I am using is defined here. When I use this function on a particular dataset I get the ...
10k views

### Extracting data.frame from simple features object in R

Is there an sf-native (i.e. "correct") way of extracting everything except the geometry column from a simple features object? This works df <- dplyr::select(as.data.frame(sf), -geometry) but the ...
51k views

### Projecting sp objects in R

I have a number of shapefiles in different CRSs (mostly WGS84 lat/lon) that I'd like to transform into a common projection (likely Albers Equal Area Conic, but I may ask for help on choosing in ...
33k views

### Clustering spatial data in R? [closed]

I have bunch of data points with latitude and longitude. I want to use R to cluster them based on their distance. I have already taken a look at this page and tried clustTool package. But I am not ...
53k views

### Clipping a raster in R

I am building a map for the northeastern U.S.A. The map background needs to be either an altitude map or a mean annual temperature map. I have two rasters from Worldclim.org which give me these ...
6k views

### How can I conduct Geographically Weighted Principal Component Analysis using ArcGIS, Python and SPSS/R?

I am after a description/methodology for conducting a Geographically Weighted Principal Components Analysis (GWPCA). I am happy using Python for any portion of this and I imagine SPSS or R being used ...
22k views

### Overlay a spatial polygon with a grid and check in which grid element specific coordinates are located

How can one use R to split a shapefile in 200 meter squares/sub-polygons, plot this grid (incl. ID numbers for each square) over the original map below, and evaluate in which square specific ...
14k views

### How to perform Random Forest land cover classification?

This is a follow-up to a previous post: Machine Learning Algorithms for Land Cover Classification. It seems that the Random Forest (RF) classification method is gaining much momentum in the remote ...
30k views

### Raster legend in R: how to colour specific values?

I have an elevation model plotted in R r <- raster("example.dem") plot(r, col = topo.colors(20)) Some of the values are below sea level (0), and I'd like to have those display in red. How can I ...
15k views

### Increasing speed of crop, mask, & extract raster by many polygons in R?

I'm extracting the area and percent cover of different land use types from a raster based on several thousand polygon boundaries. I've found that the extract function works much faster if I iterate ...
32k views

### Joining polygons in R

I am wondering how to join spatial polygons using R code? I'm working with census data where certain areas change over time and I wish to join the polygons and the corresponding data and simply ...
15k views

### Working with PostGIS data in R?

I work with R almost all the time, and now I am using it for doing spatial data mining. I have a PostGIS database with (obviously) GIS data. If I want to make statistical spatial analysis and plot ...
15k views

### How to read feature class in file geodatabase using R?

I have a feature contained in a geodatabase that is larger than 2GB as an exported shapefile. I need to run an extract function in R to attribute the polygons with data from a raster file. Exporting ...
67k views

### find the centroid of a cluster of points

While searching the web, solutions for finding centroids of polygons come up rather often. What I'm interested in is finding a centroid of a cluster of points. A weighted mean of sorts. I would ...
4k views

### Speed of various raster data formats

I'm having trouble locating any discussion or comparative benchmarking of different raster file formats (e.g., for use in data analysis in R). Does anybody have any insight into why particular formats ...
49k views

### How do I change the polygon fill color and border color for SpatialPolygons objects?

I have SpatialPolygons or SpatialPolygonsDataFrames which I'd like to plot. How do I change the color using the three plotting systems in R (base graphics, lattice graphics, and ggplot2)? Example ...
9k views

### How to speed up the plotting of polygons in R?

I want to plot the country borders of North America over a raster image depicting some variable and then overlay contours on top of the plot using R. I have been successful in doing this using base ...
12k views

### Using R to calculate the area of multiple polygons on a map that intersect with another overlaid polygon

I have a shapefile downloaded from the Ordnance Survey that gives electoral ward (division) boundaries for a county of the United Kingdom. I have successfully used R to load the shapefile and plotted ...
10k views

### Performing Raster Noise Reduction and Edge Smoothing?

I have a raster image like this I want to delete the isolated noise point and smooth the edge (say the white single points, and I want the edges of the entity look smooth). How can I do this in ...
5k views

### Using R with ArcGIS Desktop?

I've used the integration of R with QGIS, and found it a very uesful tool. Having taken a look at the R toolbox extension for 9 and the extension for 10 it appears they both offer only point ...
17k views

### Proper way to rbind SpatialPolygonsDataFrames with identical polygon IDs?

What's the proper R idiom for rbinding SPDFs together when the IDs overlap? Note that here (as is often the case) the IDs are basically meaningless so it's pretty annoying that I can't just make ...
23k views

### Merging multiple SpatialPolygonDataFrames into 1 SPDF in R?

I have created 2 polygons in QGIS. Using them in R, polygons become automatically SpatialPolygonsDataFrame (SPDF). I would like to merge them into single SPDF (as is super easy in ArcGis using Tool ...
11k views

### R convert sf object back to SpatialPolygonsDataFrame

How do I convert an sf object back to a SpatialPolygonsDataFrame? I am running R 3.4.0 and sf version 0.4.1. The sf documentation mentions an as_Spatial function, but this isn't available in my ...
14k views

### Instantiating spatial polygon without using a shapefile in R

So, the usual way we read a shapefile in R is via the maptools package, like this: sfdata <- readShapeSpatial("/path/to/my/shapefile.shp", proj4string=CRS("+proj=longlat")) However, I have a use ...
14k views

### Getting TopologyException: Input geom 1 is invalid which is due to self-intersection in R?

The 'TopologyException: Input geom 1 is invalid' self-intersection error which arises from invalid polygon geometries has been widely discussed. However, I haven't found a convenient solution on the ...
10k views

### How to Find the Average Raster Value of an area defined by a shapefile using R?

I have a set of raster images which represent a specific month over the years, and I want to make a timeline of the average values of an area using a shapefile. How do I extract the values from the ...
629 views

### Why are geolocations on Twitter “rounded”?

I'm plotting some geolocations (lon, lat) of tweets collected using the Stream API with a 7mi. radius. The picture below shows two maps of the same data, but using different alpha/transparency for ...
3k views

### Robust alternatives to Moran's I

Moran’s I, a measure of spatial autocorrelation, is not a particularly robust statistic (it can be sensitive to skewed distributions of the spatial data attributes). What are some more robust ...
5k views

### Crop simple features object in R

Is there a function to crop sf map object, similar to maptools::pruneMap(lines, xlim= c(4, 10), ylim= c(10, 15)) used for SpatialPolygon or SpatialLine? I am considering st_intersection() but there ...
4k views

### How can I generate irregular grid containing minimum n points?

Given a large (~ 1 million) sample of unevenly distributed points - is it possible to generate irregular grid (in size, but could also be irregular in shape if that is possible?) that will contain ...
23k views

### Convert a spatial polygon object to data frame using R

My goal is to modify an existing shapefile by merging certain polygons. After importing the shapefile and using the UnionSpatialPolygons command, I get the polygon outline that I want. However, this ...
29k views

### Checking if points fall within polygon Shapefile

Zillow has a set of shapefiles for different neighborhoods of major US cities. I wanted to check if certain buildings were present in certain neighborhoods using R: library(rgeos) library(sp) library(...
20k views

7k views

### Fixing orphaned holes in R

I'm trying to perform a union on a common field after merging two adjacent shapefiles. The shapefiles end up with at least one thin sliver of space between them. When I attempt a union I get the ...
2k views

### How to generalize SpatialPolygons and conserving topology in R?

I would like to generalize a SpatialPolygonsDataFrame in R to make plotting more efficient. The polygons I have result in huge pdf plots that are slow to load and hard to integrate into a document. ...
14k views

### How to create an attractive choropleth map in R?

I've been experimenting with choropleth mapping techniques in R, having reaped the benefits of ggplot2 for creating beautiful graphs within a powerful data analysis package. When it comes to mapping ...
28k views

### how to overlay a polygon over SpatialPointsDataFrame and preserving the SPDF data?

I have a SpatialPointsDataFrame with some additional data. I would like to extract those points inside a polygon and at the same time, preserve SPDF object and its corresponding data. So far I've ...
2k views

### What are sources for current geo-statistical analysis classes/events: open or proprietary

I am keenly interested in geo statistics (and many other words with 3 t's :). I am not very programatically inclined but have been following several lists with this theme. One being the R filter on ...
27k views

### Join spatial point data to polygons in R

I am trying to perform a spatial join between point data and polygon data. I have data that indicate the spatial coordinates of an event in my csv file A and have another file, shapefile B, that ...
10k views

### Read a table from an ESRI file geodatabase (.gdb) using R

I am trying to read a table directly from an ESRI file geodatabase into R. An example data file can be downloaded here. The database contains a point feature class (Zone9_2014_01_Broadcast) and two ...
1k views

### Monte Carlo simulation using QGIS and pgRouting on optimal sidewalk construction

I am new to spatial analysis and would appreciate some general direction on a project I am attempting, outlined below (I am starting from scratch). GOAL: To find the best locations to install 2000 ...