I started creating Maps through R. There are number of packages to take care of this. In this post I shall explain as how to fetch map with the help of "ggmap" and then plot it with "ggplot2".
We need a data file consisting coordinates (longitudes, latitudes) of any given terrain. In this post I am going to explain plotting points on Indian map. So, the steps are going to be:
1. Preparing a data file with logitudes and latitudes
2. Fetching map from google server
3. Plotting points on the server
Step - 1: Preparing a data file with logitudes and latitudes:
I already prepared a file which consists of coordinates and also a data column of accidents in different cities in India. You can download the file from here. The data file is basically a CSV (comma separated values) file. It is easy to import CSV file in R (you may also import Excel files but it is not strait). The following is the procedure to import the (CSV) file.
> my.df <- file.choose="" font="" read.csv="">->
> my.df
my,df is my data set (which is basically an object) in which there is data column (variable) "accidents". You may look at the variables (columns) by executing the following command in R editor.
> edit(my.df)
Step - 2: Fetching map from google server
The following procedure illustrate as how to get Indian map from google server. We need two packages I am going to invoke the libraries by using command "library"
> library(ggmap)
> library(ggplot2)
> my.map <- get_map="" india="" zoom="5)</span">->
> ggmap(my.map)
You can see Indian map popping up in graph canvas adjacent to R console. The following is the graph retrieved from google server.
Step - 3: Plotting points on the server
Now we are going to identify those cities (listed in our data file) where the accidents are identified.
> ggmap(my.map)+geom_point(data=my.df, aes(x=my.df$Longitude, y=my.df$Latitude, label=my.df$acci))
We get the following map.
But the points are not distinct, so let me make them little better by changing the color (red) and size (3.5).
> ggmap(my.map)+geom_point(data=my.df, aes(x=my.df$Longitude, y=my.df$Latitude, label=my.df$acci), color="red", size=3.5, alpha = 0.5)
The following is the map that I get with the help of above statement.
Now I guess it is better to get number of accidents just adjacent to the point. Isn't it? I can get the numbers just adjacent to the point by following statement.
> ggmap(my.map)+geom_point(data=my.df, aes(x=my.df$Longitude, y=my.df$Latitude, label=my.df$acci), color="red")+geom_text(data = my.df, aes(x = my.df$Longitude, y = my.df$Latitude, label = my.df$acci), size=3.5, vjust = 0.5, hjust = -0.75)
The following is the map that we can produce with above statement.
I hope you got the point!
How is it? Now, I am going to give you assignment! Yes, exactly.....
Delete the numbers and try to draw draw a line between any two given points on this map. You can also try on satellite map rather than a terrain map.
All the best!
We need a data file consisting coordinates (longitudes, latitudes) of any given terrain. In this post I am going to explain plotting points on Indian map. So, the steps are going to be:
1. Preparing a data file with logitudes and latitudes
2. Fetching map from google server
3. Plotting points on the server
Step - 1: Preparing a data file with logitudes and latitudes:
I already prepared a file which consists of coordinates and also a data column of accidents in different cities in India. You can download the file from here. The data file is basically a CSV (comma separated values) file. It is easy to import CSV file in R (you may also import Excel files but it is not strait). The following is the procedure to import the (CSV) file.
> my.df <- file.choose="" font="" read.csv="">->
> my.df
my,df is my data set (which is basically an object) in which there is data column (variable) "accidents". You may look at the variables (columns) by executing the following command in R editor.
> edit(my.df)
Step - 2: Fetching map from google server
The following procedure illustrate as how to get Indian map from google server. We need two packages I am going to invoke the libraries by using command "library"
> library(ggmap)
> library(ggplot2)
> my.map <- get_map="" india="" zoom="5)</span">->
> ggmap(my.map)
You can see Indian map popping up in graph canvas adjacent to R console. The following is the graph retrieved from google server.
Step - 3: Plotting points on the server
Now we are going to identify those cities (listed in our data file) where the accidents are identified.
> ggmap(my.map)+geom_point(data=my.df, aes(x=my.df$Longitude, y=my.df$Latitude, label=my.df$acci))
We get the following map.
But the points are not distinct, so let me make them little better by changing the color (red) and size (3.5).
> ggmap(my.map)+geom_point(data=my.df, aes(x=my.df$Longitude, y=my.df$Latitude, label=my.df$acci), color="red", size=3.5, alpha = 0.5)
The following is the map that I get with the help of above statement.
Now I guess it is better to get number of accidents just adjacent to the point. Isn't it? I can get the numbers just adjacent to the point by following statement.
> ggmap(my.map)+geom_point(data=my.df, aes(x=my.df$Longitude, y=my.df$Latitude, label=my.df$acci), color="red")+geom_text(data = my.df, aes(x = my.df$Longitude, y = my.df$Latitude, label = my.df$acci), size=3.5, vjust = 0.5, hjust = -0.75)
The following is the map that we can produce with above statement.
I hope you got the point!
How is it? Now, I am going to give you assignment! Yes, exactly.....
Delete the numbers and try to draw draw a line between any two given points on this map. You can also try on satellite map rather than a terrain map.
All the best!
Thanks a lot, Mr. Kamakshaiah. It was quite difficult to find a good source on the internet.
ReplyDeleteSuraj
This comment has been removed by the author.
ReplyDeleteHi, Thank you for the post!!!.
ReplyDeleteCan we increase the size of the point relative to number of accidents. I mean more the accidents bigger the size of the bubble?
my.map <- get_map="" india="" zoom="5)
ReplyDeleteis there a problem with this line of code