1

I am plotting India's district map with data (cases). First I read data for each district and district shapefile. Later I merge the two on district. Using fortify data I get ggplot. I get a beautiful plot using ggplot.

nr1 <- read.csv("../OUTPUT/working_districts_state.csv")
india.dist <- readShapeSpatial("./shapes2/2011_Dist.shp")
india.dist@data <- merge(india.dist@data,nr1[,c(1,3)],by="DISTRICT")
india.dist@data$id <- rownames(india.dist@data)
ss.fort <- fortify(india.dist)
ss.fort1 <- merge(ss.fort, india.dist@data, by="id")

d <- ggplot() + geom_polygon(data = ss.fort1, aes(x = long, y = lat, group = group, fill=reg.cases)) +coord_equal()

I have two issues:

First, I want to plot a state separately. I sub-set the shapefile and try to plot using similar code. Problem arises when I use fortify command. The command creates an id column with ID extracted from original data i.e. india.dist and not the sub file. So when I merge state.fort with state@data, I do not get proper merge. I am still perplexed as I do not know how to reset ID.

kr <- india.dist[india.dist$ST_NM== "Kerala",]
state <- kr
state@data$id <- rownames(state@data)
state.fort <- fortify(state)
state.fort1 <- merge(state.fort, state@data, by="id")
unique(state.fort[,c(4:7)])
     hole piece  id group
1    FALSE     1   7   7.1
363  FALSE     1 173 173.1
719  FALSE     1 232 232.1
1384 FALSE     1 277 277.1
1960  TRUE     2 277 277.2
2013 FALSE     1 288 288.1
2561 FALSE     1 314 314.1
3018 FALSE     1 320 320.1
3349 FALSE     1 321 321.1
3753 FALSE     1 358 358.1
4144 FALSE     1 422 422.1
4735 FALSE     1 436 436.1
5204 FALSE     1 567 567.1
5663 FALSE     1 571 571.1
6292 FALSE     1 610 610.1

state@data
             DISTRICT  ST_NM ST_CEN_CD DT_CEN_CD censuscode reg.cases id
1           Alappuzha Kerala        32        11        598       259  1
2           Ernakulam Kerala        32         8        595       769  2
3              Idukki Kerala        32         9        596        42  3
4              Kannur Kerala        32         2        589        50  4
5           Kasaragod Kerala        32         1        588        38  5
6              Kollam Kerala        32        13        600       381  6
7            Kottayam Kerala        32        10        597        63  7
8           Kozhikode Kerala        32         4        591       237  8
9          Malappuram Kerala        32         5        592        95  9
10           Palakkad Kerala        32         6        593        31 10
11     Pathanamthitta Kerala        32        12        599        30 11
12 Thiruvananthapuram Kerala        32        14        601        97 12
13           Thrissur Kerala        32         7        594        99 13
14            Wayanad Kerala        32         3        590        39 14

Secondly, I suspect that the ggplot is not matching data (cases) to district properly. It is mismatching the data. I do not know how to check that such mismatch is not happening and if it is happening then how to fix it. I think this issue also involves handling ID in shapefile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.