How To Upload Shapefile Into R
If yous did not discover the geospatial information you lot demand in existing R packages (encounter the map section), you need to find this data elsewhere on the web.
Usually, you will find it as a shape file format. This format is composed by several files that you need to go along together in the aforementioned folder.
Note: if yous found a .geoJSON
file, read this post instead.
Discover and download a shapefile.
Y'all need to dig the internet to find the shape file you lot are interested in. For instance, this URL will redirect yous to a zipped shape file containing the worl boundaries.
You tin can download it and unzip it with R:
Read it with rgdal
The rgdal
package offers the readOGR()
function that allows to read shapefile using the following syntax.
As a effect you become a geospatial object (my_spdf
here) that contains all the information we need for further mapping. Please try th following command to understand how this object works:
-
summary(my_spdf)
: tells y'all the max and min coordinates, the kind of projection in use -
length(my_spdf)
: how many regions you lot have -
head(my_spdf@data)
: the firs few rows of the data slot associated with the regions
Plot information technology with base R
The basic plot()
function knows how to plot a geospatial object. Thus yous simply need to laissez passer information technology my_spdf
and add a couple of options to customize the output.
Plot it with ggplot2
It is totally possible (and advised imo) to build the map with ggplot2. However, ggplot2 takes as input data frames, not geospatial data.
my_spdf
thus needs to exist transformed using the tidy()
function of the broom
package. The region
argument of this function wait 1 of the column name if the @data slot. It will exist the region name in the new dataframe.
In one case the data frame is created, it is plotted using the geom_polygon()
function as described below.
Source: https://r-graph-gallery.com/168-load-a-shape-file-into-r.html
Posted by: joynerjokened.blogspot.com
0 Response to "How To Upload Shapefile Into R"
Post a Comment