1

I want to generate a map of India in R. I have five indicators with different values of every state. I want to plot bubbles with five different colors, and their size should represent their intensity in every state. For example:

State A B C D E
Kerala - 39, 5, 34, 29, 11
Bihar - 6, 54, 13, 63, 81
Assam - 55, 498, 89, 15, 48,
Chandigarh - 66, 11, 44, 33, 71

I have gone through some links related to my problem:

[1] http://www.r-bloggers.com/nrega-and-indian-maps-in-r/

[2] An R package for India?

But these links could not serve my purpose. Any help in this direction would be greatly appreciated.

I have also tried

library(mapproj)
map(database= "world", regions  = "India", exact=T, col="grey80", fill=TRUE, projection="gilbert", orientation= c(90,0,90))

lat <- c(23.30, 28.38)

lon <- c(80, 77.12) # Lon and Lat for two cities Bhopal and Delhi

coord <- mapproject(lon, lat, proj="gilbert", orientation=c(90, 0, 90))

points(coord, pch=20, cex=1.2, col="red")

In nut shell problems are: (1) It does not give me plot at district level. Not even boundries of states. (2) How to create bubbles or dots of my data in this plot, if I have only name of locations and corresponding value to plot? (3) can this be done in easily in library(RgoogleMaps) or library(ggplot2)? (Just a guess, I do not know much about these packages)

Community
  • 1
  • 1
Pankaj
  • 1,296
  • 2
  • 13
  • 23
  • 1
    Do you really need to add five bubbles in each state? That seems to take up quite a bit of space; you may not have enough space to add five bubbles for some small states. I wonder if you may want to have five separated India maps. – jazzurro Apr 25 '15 at 08:42
  • Can you provide the code by which you created the map of India? – lawyeR Apr 25 '15 at 09:21
  • Dear lawyeR & jazzurro, I followed the codes from above links, as i do not have any knowledge about geographical maps in R. But I could not find these codes working for me. You are right in saying that map would have less space, specially for smaller states. I will try to manage with three indicators. – Pankaj Apr 25 '15 at 10:22
  • The answers you linked were outdaed because the GADM links have changed. I have edited them (the edits should be live soon), and this should work to get you started on plotting the map of India: http://www.r-fiddle.org/#/fiddle?id=zJd1RfjS – Molx Apr 25 '15 at 18:09
  • @Molx: thank you for all your work and the code on r-fiddle. The final line returned an error: > IND <- getCountries("IND",level=1) Error in changeGADMPrefix(theFile, fileName) : could not find function "spChFIDs" – lawyeR Apr 26 '15 at 00:12
  • I also downloaded the shape file for India from the link http://www.diva-gis.org/gdata It is a zip file. I extracted it in my working directory. Then I executed the following lines `library(maptools)` `India <- readShapeSpatial('IND_adm1.shp')` `plot(India)` . It gives me a plot for India, but aftermath i do not know. – Pankaj Apr 26 '15 at 05:14
  • @lawyeR I forgot to save the addition of `library(sp)` to the beggining of the R-Fiddle code. That's where `spChFIDs` is. – Molx Apr 26 '15 at 13:40
  • Also posted at http://gis.stackexchange.com/questions/144003. – whuber Apr 27 '15 at 13:38

2 Answers2

3

As @lawyeR states, a choropleth (or thematic) map is more commonly used to represent variables on a map. This would require you to produce one map per variable. Let me take you through an example:

require("rgdal")  # needed to load shapefiles

# obtain India administrative shapefiles and unzip
download.file("http://biogeo.ucdavis.edu/data/diva/adm/IND_adm.zip", 
              destfile = "IND_adm.zip")
unzip("IND_adm.zip", overwrite = TRUE)

# load shapefiles
india <- readOGR(dsn = "shapes/", "IND_adm1")

# check they've loaded correctly with a plot
plot(india)

# all fine. Let's plot an example variable using ggplot2
require("ggplot2")
require("rgeos")  # for fortify() with SpatialPolygonsDataFrame types

india@data$test <- sample(65000:200000000, size = nrow(india@data),
                          replace = TRUE)

# breaks the shapefile down to points for compatibility with ggplot2
indiaF <- fortify(india, region = "ID_1")
indiaF <- merge(indiaF, india, by.x = "id", by.y = "ID_1")

# plots the polygon and fills them with the value of 'test'
ggplot() +
  geom_polygon(data = indiaF, aes(x = long, y = lat, group = group,
                                  fill = test)) +
  coord_equal()

Finally, I notice you asked the same question on GIS SE. This is considered bad practice and is generally frowned upon, so I've flagged that question to be closed as a duplicate of this. As a general rule of thumb try not to create duplicates.

Good luck!

Phil
  • 4,344
  • 2
  • 23
  • 33
  • Oh sorry! I did not know this. I will take care of this in future. Thanks Phil. – Pankaj Apr 27 '15 at 12:51
  • 1
    That's fine, it takes a while to get the hang of things, so I hope to be helpful rather than judgemental :) – Phil Apr 27 '15 at 13:05
  • Thanks for great answer. Note that if you are having problems with readOGR, `india <- readOGR(dsn = "shapes/", "IND_adm1")` needs `IND_adm1.shp` file in directory `shapes`. Default unzip location for me wasn't `shapes/`, I manually put extracted files in `shapes` folder. I also had to change `dsn = "shapes/"` to `dsn = "shapes"`. – Deep Apr 07 '21 at 06:28
  • If you only want states data (most useful case ig), you can remove lines starting with `india@..` (which is for testing only anyways) and `indiaF <- merge(..`. Check `indiaF` now, you will have column called `id`, which are id numbers of the states. You can check `IND_adm1.csv` to see which id belongs to which state. If you've removed `test`, just change `fill = test` with `fill = id` to see each states colored differently. Now you can color them according to your numerical variable by changing `fill = `. Using `RColorBrewer` will be best option for choropleth maps. – Deep Apr 07 '21 at 06:34
1

Once you have the shapefile for India, you need to create a choropleth. That will take the shapefule map and color each State in India on a gradient that reflects your data. You may want to create a panel of five plots, each one showing India and its states colored according to one of your five variables.

For others who can push this answer farthr, here is the dput of the data frame, after a bit of cleaning.

dput(df)
structure(list(State = c("Kerala", "Bihar", "Assam", "Chandigarh"
), A = c("39", "6", "55", "66"), B = c("5", "54", "498", "11"
), C = c("34", "13", "89", "44"), D = c("29", "63", "15", "33"
), E = c("11", "81", "48", "71")), .Names = c("State", "A", "B", 
"C", "D", "E"), row.names = c("Kerala", "Bihar", "Assam", "Chandigarh"
), class = "data.frame") 
lawyeR
  • 7,488
  • 5
  • 33
  • 63