BASIC MAPPING IN R

Agasthya Naib Guruprasad

2020-04-03

INTRODUCTION

As a data scientist at some point in time, we will encounter data sets that need to be addressed in maps, these data are called geospatial datasets. For example, a demographic map might help in visualizing political datasets such as the united states presidential elections 2016 ( USelections ). It can also be used to map natural calamities such as forest fires, droughts, rainfall along with the impacts using color-coding of maps.

So, now let’s go through the basic of plotting point/location on a map

How to find the location

Location on a map can be considered as a point of interest denoted by latitudes and longitudes. For this data, you can import the datasets having the latitudes and longitudes.

Libraries needed

We have used maps and map data library as the name itself suggests their functionalities. JSON is the file extension I will be using in the vignette

library('rjson')  #similar to .xlsx, .pdf, .doc, .csv
library('maps')
library('mapdata')

Here JSON is a file extension which stands for JavaScript Object Notation which is used to pull in data just like a .csv file (for more info: click here )

In this vignette we will try to find our current location and map it

Every device has a Global positioning system if not there are several APIs like:
1.Google’s Places API
2.HTML5’s geolocation API
or, Location-based on wifi and cell towers. These API maps the computer’s IP address to Latitude and Longitude.

In this case, we will find the current position using our IP address. by using this https://api.ipify.org?format=json we can get the IPv4 as a JSON file from where the IP address can be extracted.

myIPaddress <- readLines("https://api.ipify.org?format=json", warn = FALSE)
ipaddress<-fromJSON(myIPaddress)  
getGeoString <- paste0("https://ipinfo.io/", ipaddress$ip[1],"/json")
location<-fromJSON(file=getGeoString)
pos<-strsplit(location$loc,",") #here the json file is read and "," is the delimeter loc is one of the variables
print(pos)# latitude and longitude will be printed
## [[1]]
## [1] "-33.8678" "151.2073"
latitude<-as.numeric(pos[[1]][1])
longitude<-as.numeric(pos[[1]][2])
print(latitude)#prints the latitude
## [1] -33.8678
print(longitude)#prints the longitude
## [1] 151.2073

So, the result of the above chunk of code has provided us with the approximate position w.r.t latitude and longitude.

A range can be ‘given1

range<-10

Mapping the position

map("world",regions = "Australia", #map func from maps library
    xlim = c(longitude-range, longitude+range),#xlim and ylim i.e the 
    ylim = c(latitude-range, latitude+range),
    col = "white", fill = TRUE,
    mar = c(1.1, 1.1, par("mar")[3], 2))
points(longitude,latitude,col="blue", pch = 10, cex = 2)
title(paste(location$city, ", ", location$region, " - ", location$country))
map.cities( label = TRUE, minpop = 500000)

In the above chunk of code, a map function is called in which an inbuilt dataset " world" is used and the x and y limit is provided for both the longitude and the latitude the color of the map is filled in white. Points are used to create a colored point on the map providing us with the position of the user-device the title for the map is given the nearest city is named.

Now let us create a ‘world map’

library('ggplot2') #let's try using ggplot
library('dplyr')
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

Here two libraries ggplot2 as well as the dplyr

world_map<-map_data('world') #the map data is in world dataset
ggplot(world_map,aes(long,lat,group=group))+geom_polygon(fill="lightgray",color='white')+geom_point(aes(longitude,latitude),color="black",pch = 10, cex = 6)+labs(title = "World Map",subtitle = paste0("your current location :",location$country,",",location$region,",",location$city," 'shown by the marker'."))

Wordld_map is assigned the dataset world provided by the maps library which is later plotted on ggplot using geom_polygon where the map color is provided. The geomp_point uses the generated latitude and longitude to find the location of the user and mark it as a point which u see as a black marker pointing towards ‘Sydney’.

latitude=runif(1,min=-90,max=90)
print(latitude)
## [1] 74.51535
longitude=runif(1,min = -180,max=180)
print(longitude)
## [1] -151.6574
ggplot(world_map,aes(long,lat,group=group))+geom_polygon(fill="lightgray",color='white')+geom_point(aes(longitude,latitude),color="black",pch = 10, cex = 6)+labs(title = "World Map")

The runif command produces a random number where the attributes include the length of the vector, the minimum number, and the maximum number. As you can see the latitudes and longitudes are assigned based on their range. The same gglot command is called and the random location is marked on the map in black.

Map of ‘Australia’ marking its major cities

Aus<-read.csv('C:\\Users\\Agasthya\\Desktop\\pop_AUS.csv')

head(Aus)
##         ï..cities longitude  latitude population
## 1          sydney  151.2073 -33.86780    4630000
## 2 South Melbourne  144.9667 -37.83333    4170000
## 3        Brisbane  153.0243 -27.47101    1860000
## 4           Perth  115.8614 -31.95224    1532000
## 5        Adelaide  138.5986 -34.92866    1145000
library(leaflet) #it is an open scource library 
#devtools::install_github("rstudio/leaflet")
 m <- leaflet(options = leafletOptions(minZoom = 0, maxZoom = 18)) %>%
   addTiles() %>%  # Add default OpenStreetMap map tiles
    addCircles(lng = Aus$longitude, lat = Aus$latitude, weight = 1,
    radius = sqrt(Aus$population) * 300, popup = Aus$cities)
m

the above map is zoom enabled

By using the map library which was loaded previously and set the data and the region of interest we were able to produce the map of Australia along with these parameters, The longitude range, and latitude ranges were specified along with the margins. The size of the blue circle determines the population of the region.Hence, Sydney and Melbourne which are the two most populated cities in the country have larger circles.

CONCLUSION

Hence by obtaining the latitudes and longitude, we will be able to plot the approximate position on the map. This mapping can be further enhanced while trying to estimate global changes such as population explosion, climatic changes, financial growth concerning a region, etc where geospatial data is found.
More attractive mapes can be made using ggmap which is a Google API.

REFERENCES

1.https://stackoverflow.com/questions/37980290/map-australian-cities-r-spatial
2.https://cran.r-project.org/package=maps
3.https://rstudio.github.io/leaflet/


  1. it is difficult to find the approximate position hence a +/- of 10 is given for accuracy