The

Art

Bay

R Spatial Package

The need for spatial data analysis has never been greater! With the explosion of mobile technology, vast amounts of location information are being gathered at an unprecedented rate. Whether it’s tracking your phone to determine where you work or how long it takes you to get from place to place, GPS trackers that monitor vehicle movement and speed, or monitoring social media to see who and what you talk about, all of this information provides a wealth of insights for businesses to use for marketing, planning, and creating better strategies and operations.

Spatial analytics are just as important because they go beyond looking at individual pieces of data. Rather than analyzing each piece independently, these analytical techniques look at relationships between different parts of the dataset to produce more meaningful results. For example, if you were trying to find the best restaurant near your house, knowing only whether there was a good movie theatre next door would not tell you much. But seeing both variables about each other can help you make a decision.

Fortunately, we have some great software available to us now that makes doing spatial analyses easy-t0-say-advanced. Systems like QGIS, ArcMap, and Google Maps already include many powerful tools that facilitate spatial research. More specialized applications also exist, such as those designed to analyze disease spread patterns or evaluate the safety of proposed construction projects.

Introduction to the R Spatial package

r spatial package

The other major category of packages for rstats.rs is spatial or location-based statistical software. This includes ggplot2, lattice, and others that create interactive maps. These tools are very popular because they allow you to add context to your data by showing where an event happened (is it likely in areas with a higher crime?) or determining how close people are to each other depending on what questions you have.

These types of applications are powerful ways to visualize your data. However, they require some level of mathematical sophistication to use correctly. For example, when creating graphs using ggplot2, you will need to know about coordinate systems, transformations, etc. Luckily there are many free resources online and through universities for those who want to take their graphing skills further!

Fortunately, most of these programs are built around the same set of functions and work similarly. That makes it easy to learn once and then apply them to different projects. Two common ones that come up in almost any field are mapping and GIS (or geographic information science). Having this knowledge can help you do more advanced things like determining if someone lives near a hospital or college so you can develop community services or even find potential criminals.

Obtaining the R Spatial package

r spatial package

You can download the R Spatial package directly from GitHub here! Simply click the Download button to get started. Once it is downloaded, you can install it using either of your software’s built-in installation tools or via the Ubuntu Software Center.

The best way to use this powerful tool depends on what kind of spatial analysis you want to perform. If you are just getting into GIS, then starting with simpler functions in the package may be enough. For example, if you wanted to determine how close different areas are to each other, then “calculate distance between two points” would be an excellent first function to learn.

By learning these simple functions, you will have laid a strong foundation for more advanced functionality in the future. This could include finding out how many streets fall within a certain radius of a given location, determining whether there are too many supermarkets in a specific area, or even looking at which neighbourhoods tend to flood during heavy rains.

All of these types of analyses require some sort of coordinate system, so make sure to become familiar with them before moving on to the next one.

Loading the R Spatial package

r spatial package

Once you have installed the R spatial packages, you can use them to do some cool things! For instance, say you want to know where all of the major roads are in your city or state. You could create a spatial object using either Google Maps, OpenStreetMap, or another mapping website and then run which locations have been selected as parts of that object.

For this example, we will be using OSM data.

Understanding R Spatial objects

r spatial package

An important concept when working with spatial data in R is understanding what an object represents. For example, if you have county borders as a dataset, then what does each row represent?

It could be one side of a border between two counties, or it could be part of a larger state boundary or even a national boundary! Therefore, to properly load this into R, we need to know which package contains that information.

The r geos library is just for geographical shapes like polygons and lines. It doesn’t contain any data about countries so it can’t help us there. However, it does include functions to manipulate those boundaries, so we will use that to our advantage.

Running through all of these steps using the above-mentioned datasets would take quite some time so let’s make them faster by using the database() function within the RDA library. This allows us to run queries against different layers of spatial data without having to re-download them every time!

We can also add parameterized statements such as “IN (value1, value2)” which checks if both values are included in the set, and “OR (value1, value2)” which adds the second value only if the first isn’t already present.

Understanding R spatial data types

r spatial package

As mentioned before, rgdal is an excellent way to read shapefiles into R. It also makes sense to learn what each field in these files means as it can be used for other file formats such as KML or GeoJSON.

One of the most confusing things about using rgdal is determining which type of object you have access to within your layer. For instance, if you open up a country layer with counties, some might only contain polygons while others contain points or lines.

Luckily, there are some definitive ways to determine this information so that you do not need to take time to look up how to identify them.

Understanding raster objects

r spatial package

An important concept when working with spatial data in r is understanding what kind of object you have. For example, if your dataset consists of land cover classification maps or natural resource images (e.g., forests, rivers), then you will need to know what level of detail each image contains and whether those details are binary (0-1) or trinary (0-2-3).

This distinction comes down to how we interpret the map. A binary raster picture would show only two values – either it is covered by water, or it is not. A trinary one would show three different levels of coverage (water, wetlands, grasslands). The number in between shows which areas could potentially be both water AND vegetation/grassland.

When talking about rasters in general, there are two types of numbers that they can contain — quantities and proportions. Quantities simply state how much of something is present whereas proportions describe how much of each thing there is.

For instance, a proportion of 0% means nothing covers the area, while a proportion of 100% means all of the areas are covered. A ratio of 50% means half of the area is covered with just enough grass to grow crops!

Another way to look at proportions is to think of them as an intensity value. If there was no rain today, then there wouldn’t be any foliage growing so the percentage of dry soil out is low.

Understanding sp objects

r spatial package

What is an sp object? An sp object is a spatial relation that can be defined between two other things. For example, you could say there is a parent-child relationship between the name of a company and its CEO. The CEO is the child element, and their name is the parent element.

Similarly, in geography, the word ‘city’ has parents and children. A city like New York is the parent, and all cities within the United States are the children of New York.

In statistics, the term parent refers to a variable that predicts another one. If we were doing regression analysis, for instance, the parent would be age while the predicted dependent variable would be income. Older people tend to have higher incomes than younger people.

Now let’s look at some basic functions using objects!

Identifying related rows with relate ()

We will use the dataset restaurant as our source material. This consists of 10 variables, 2 of which are factor labels (restaurant category and cuisine) and eight numeric values (price, quality, atmosphere, service, value, location, and tip).

Relation identification or Relate Analysis looks into each row of your data set and sees if any of the columns contain a match with either a parent column or a child column. In this case, we want to see whether there is a parent-parent, parent-child, or child-child relationship.

Understanding maps

r spatial package

A map is a picture or illustration of an area and/or thing broken down into discrete pieces (boxes, lines, etc.). Maps have been used to show areas and things for centuries!

Maps are very useful tools that help us understand complex concepts in geography and science. They can also be pretty fun to look at.

You may have seen a map before mentioned in a book, article, lecture, or TV program about a particular topic. These illustrations play an important role in educating people on the subject.

Luckily for you, there’s no need to learn mapping software such as Google MapSketcher or Cartographer.com to make your maps! You can create easy, free maps in any major drawing app like Microsoft PowerPoint, Adobe Photoshop, or GIMP.

This article will go over some basic steps on how to make a map using one of these apps. After that, we’ll take our new map-maker skills and apply them towards making a practical, educational map.

Privacy PolicyTerms and Conditions
Copyright © 2026 The Art Bay