Data Visualization in Map with R

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi i'm siddha and in this video i will explain about visualizing data in country maps so the data files that are required for making such country maps in my example and the our course are provided in the description of this video so let me start let me first invoke the required packages in r in r we can visualize the data in our country map with a few line of codes so so the required packages are cartography uh sf and tidy verse if you haven't installed these packages you can just install uh by typing install dot install dot packages install dot packages then sf and you can run this command and it will install the sf package in your our program so i invoked the package cartography then sf and then tidy words so the first step in visualizing the data is setting the working directory in r it's important because uh i will import data from this folder and it will save all the outputs in that folder if we if we ask r to save something it will save the output in this folder so to to say the working directory we just use the set wd command and inside parenthesis we we give the address of our working directory so i have put the data for this presentation uh in desktop and inside the mfe folder for instance dextrov and mav folder map folder is here so i just open this folder and i can copy the path of this directory from here from the address bar by clicking at the address bar i can just copy the address and i can paste it here one important thing here is that we should convert the backwards last to forwards less so we should convert the backwards less to forwards less so if i run this command stata sorry r will set the working directory so the next step is that we should download the save file for our country so the save files are the specific file formats that is stored the longitude and latitude of the country uh boundary and the boundary of each of its sub-regions so you can just google to find your country say files for instance in case of nepal if i google save file for nepal it will show various number of addresses uh that have the country set files uh one save file that i found useful was at open was at the code for nepal.com so if i open this link so it will it will so the save pile that has the district courts the district numbers the district shape area and other geometrical dimensions so we should download this save file and if i open this this folder contains a number of files a number of files uh one in dvf format another in ssp format another in cpg format and another hs experiment so we will use or our program will use the ssp file and dbf file for producing maps and for visualizing the data so after downloading this save files and unzipping them unzipping them and putting them in our working directory so the files looks like this i have i have unzipped the files and put them here in my working directory which is map so after unzipping the files i go to r again and i read the files so the the the command is st underscore read and the name of the save file in ssp format so i read the ssp data file and i run this command now the data folder or the data file is in my r environment if you open this file uh this file has information about the name of the district district court safe area and the most important one is geometry which includes the the boundary longitudes and latitudes for each districts so after importing the ssp file in r uh now this file does not have the required variable of interest for instance if we if we want to visualize the covet cases by districts we have to import a separate file and combine with this file so in my case i want to visualize the property rates by this tricks so i have i have put a separate file called poverty.dta which contains information about district wise poverty rates in nepal so this is a stata file so i use the haven library to import the data so i read the poverty data and if we look at the poverty data it contains information about district name and the poverty rate so i convert the poverty rate into percentages by multiplying it by 100 and a separate variable has been created which is named as pob and the poverty has been converted to percentage terms so now i combined both the files and uh name it as map data so the map data file combines combines the data file that we have imported from the save file the data file and the poverty file so we we we can do it by using the morse command m e r g mars the first data file name comma the second data file name so we we will mars this by the district name because district name is unique variable that is in both data sets so we we combined the data the data the same data that has geographical information system and the poverty data this we merge these two files by district name so if i run this command and if we look at the map data which is a combined version of both the files we can see that now the mave data file has information about geometrical shapes of the districts as well as the probability rate in percentage terms so we will use both these information to produce our map as well as to visualize poverty in the map so now we can now plot the map of nepal so the the command is simply plot sd underscore geometry and the name of the data file so if i run this command the map of nepal is produced in r so this is a blank map no color no districts by poverty rates no legend no title so this is blank map so now we will fill uh we will fill color by property rate in each district we will put a title and we will put legend etc so now we we can simply fill the color uh in districts by the poverty rate so the appropriate command is core layer x is map data so the name of the data file is here variable is property so we will fill color by the property rate method is quantile so if if we put method equals to quantile it will plot the color by quantile groups and the number of classes for so we will divide the poverty rates into five classes and color the districts accordingly so if if i run this command now the map is colored according to the intensity of property so it is divided the property into five groups and then colored the districts so the the districts with lower property rates are lighter and the districts with higher probability rates are darker dark blue color so now we we can add some further features for instance we can simply increase the groups increase the groups now eight groups this can be this can be done by simply changing the number of classes to eight from five and we can put the legend title as property and even we can put the title uh as the layer out layout layer title and then we can put the chart in frame inbox so now this is a basic map of nepal that has districts filled according to the intensity of poverty so the map can be further modified further made attractive by using the gg plot package so now we move to the gg plot package so in gg plot package we need simply the data file and the geometric shape and other aesthetics so ggplot command produces gzplot their data is map data gmsf and we fill we fill the districts by property and then this is simply for the color option and we put the x-axis level as longitude and y axis level as latitude and we will put the title of the map as probability rate of nepal by districts so if i run this command if i run this command the map of nepal will be produced here the the color scheme is a little bit different from the previous one so the the districts with yellow color are those districts with higher property rates and the districts with the dark purple are the district with the lower poverty rates so we can add further aesthetics for the features to this map for instance now we want to if we want to visualize the data and the name of this trick here uh we will do a simple trick we will we will first we will first extract the centroids of the of the district boundaries by using the st centroid command and we will save them in sd coordinates so if i run this command it extracts the centroid of the map data geometry and saves it in sd coordinates and combines it with the map data so if if you look at a point if we if you look at the point data file if you look at the points data file now uh we can just see that we can just see that we have added the x and y variable here x and y variable here uh by using the points command and then now we can plot the map with the data labels so so now to change the background color background graphics and other things uh we invoke the gg themes library digi themes package here and again the ggplot command the ggplot command name of the data file aesthetics we fill the districts by the pob pob means district wise poverty the color the color of the boundary black side 0.2 and this one is for the color option and we put the labels so we put two labels in in first level we put the name of the district and in the second we put the poverty rate and and we label the x axis we level the y-axis and we give a title of the chart and then we change the theme of the background the background theme we fill light blue and uh the plot title we change the color of the plot title at the rate size 16 and it's just 0.5 we we keep it bold italic font and for x axis this is the color for x axis blue color 10 point font world form and this is for y y-axis raid color 10-point font and boldface so if i if i run this command if i run this command it will produce the map of nepal with with the labels as name of districts and property red so i i run this command and i zoom in the map and here it is so it has the map of nepal with the name of the district and the poverty rate for instance uh the district the district bajura has the highest poverty rate of 64.1 which is in yellow color and under the district with the lowest forward rate the lowest poverty rate is here with the dark dark blue color which is cos key and probably rated per percent so so now you can change the aesthetics you can change the color scheme you can change the background change the font size and you can add some other aesthetics in this graph so as to make it more attractive so finally i can save this map by by the ggsafe command in the working directory first let me delete if there is any file produced earlier and now i run this command and it will save this map as map.png so you can even save this map as pdf for that you just need to write map.pdf and it will produce a pdf file in our working directory so this map that pdf so this is the file that we have produced so now you can download the save file of your own country and you you can produce or you can visualize the data of any variable by sub-regions by provinces by states or even by contents if you have the data regarding those countries those regions and states so i have i have put the command file the r file and the data file for nepal so that you can practice creating this map thank you for watching this video in the next video i will explain about how to produce the world map uh and visualize the kobe data in that map by using the save files and our program you
Info
Channel: Siddha Raj Bhatta
Views: 2,596
Rating: undefined out of 5
Keywords: siddhabhatta, data visulaization with R, tidyverse, spatial data, R studio, map data, poverty, nepal, learn R, learn R studio, siddha, bhatta, NRB, R users group nepal
Id: f26U2kwAWkQ
Channel Id: undefined
Length: 20min 3sec (1203 seconds)
Published: Fri Nov 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.