1

I have downloaded gadm map for India (SpatialPolygonsDataFrame). When I use spplot it draws the complete map of India alongwith State boundaries.

A. Is it possible to just draw the boundary of one State (say, Assam) and not of others?

B. Can I write the names of States inside their respective boundaries?

C. Or suppose I want to color only one particular State and not others

Will be grateful for help. Thanks.

(A sample code is as below. But it plots full country and all States. What if I want only one State to be plotted inside the country)

library(sp)  
library(RColorBrewer)

load("IND_adm1.RData")
abc <- read.csv("crime.csv", header = TRUE)
temp <- abc[,c(1,4)]
State = temp[1:38,1]
Offence = temp[1:38,2]
afg <- data.frame(State, Offence)
gadm$Offence <- afg[1:35,]
col <- brewer.pal(n = 9, name = "OrRd")
spplot(gadm, "Offence", col.regions = col, at = c(0,50,100,200, 500,1000, 1500,2000,2500,4000), main = "Offence")
jazzurro
  • 23,179
  • 35
  • 66
  • 76
Ashok K Harnal
  • 1,191
  • 2
  • 15
  • 28

1 Answers1

7

Let me apply the great lesson of @hrbrmstr to this case. You can learn a lot from him. The link is here.

library(raster)
library(rgdal)
library(rgeos)
library(ggplot2)
library(dplyr)

### Q1: Assam only

### Get data
india <- getData("GADM", country = "India", level = 1)

### Choose Assam
assam <- subset(india, NAME_1 == "Assam")

### SPDF to DF
map <- fortify(assam)

### Draw a map

ggplot() +
geom_map(data = map, map = map, aes(x = long, y = lat, map_id = id, group = group))

enter image description here

### Q2: State with their names

map <- fortify(india)
map$id <- as.integer(map$id)

dat <- data.frame(id = 1:(length(india@data$NAME_1)), state = india@data$NAME_1)
map_df <- inner_join(map, dat, by = "id")

centers <- data.frame(gCentroid(india, byid = TRUE))
centers$state <- dat$state


### This is hrbrmstr's own function
theme_map <- function (base_size = 12, base_family = "") {
theme_gray(base_size = base_size, base_family = base_family) %+replace% 
theme(
axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.ticks.length=unit(0.3, "lines"),
axis.ticks.margin=unit(0.5, "lines"),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.background=element_rect(fill="white", colour=NA),
legend.key=element_rect(colour="white"),
legend.key.size=unit(1.5, "lines"),
legend.position="right",
legend.text=element_text(size=rel(1.2)),
legend.title=element_text(size=rel(1.4), face="bold", hjust=0),
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.margin=unit(0, "lines"),
plot.background=element_blank(),
plot.margin=unit(c(1, 1, 0.5, 0.5), "lines"),
plot.title=element_text(size=rel(1.8), face="bold", hjust=0.5),
strip.background=element_rect(fill="grey90", colour="grey50"),
strip.text.x=element_text(size=rel(0.8)),
strip.text.y=element_text(size=rel(0.8), angle=-90) 
)   
}

ggplot() +
geom_map(data = map_df, map = map_df,
         aes(map_id = id, x = long, y = lat, group = group),
         color = "#ffffff", fill = "#bbbbbb", size = 0.25) +
geom_text(data = centers, aes(label = state, x = x, y = y), size = 2) +
coord_map() +
labs(x = "", y = "", title = "India State") +
theme_map()

enter image description here

### Q3: Add color to one state

### Create a column for color. Assam will have a different color.
map_df2 <- transform(map_df, hue = ifelse(state == "Assam", "a", NA))


ggplot() +
geom_map(data = map_df2, map = map_df2,
         aes(map_id = id, x = long, y = lat, group = group, fill = hue),
         color = "#ffffff", size = 0.25) +
geom_text(data = centers, aes(label = state, x = x, y = y), size = 2) +
coord_map() +
labs(x = "", y = "", title = "India State") +
theme_map() +
theme(legend.position = "none")

enter image description here

jazzurro
  • 23,179
  • 35
  • 66
  • 76
  • Many thanks for so much trouble. It will take me some time to understand all this but I will go through each line. – Ashok K Harnal Feb 05 '15 at 00:51
  • @user3282777 Pleasure. Take your time and get used to this kind of task. You may want to check how data looks like when you go through the lines. In that way, you will see what is happening. – jazzurro Feb 05 '15 at 00:55