Comparison of only one state of India and the spelling of its name within the state border
I have uploaded a gadm map for India (SpatialPolygonsDataFrame). When I use spplot it draws a complete map of India along the state lines.
and. Is it possible to simply draw the border of one state (say, Assam) and not others?
Q. Can I write the names of states within their respective boundaries?
C. Or suppose I want to color only one particular state, and not others.
We will be grateful for your help. Thank.
(The example code is as shown below, but it covers the whole country and all states. What if I want only one state to be built in a 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")
0
source to share
1 answer
Let me apply @ hrbrmstr's great tutorial to this case. You can learn a lot from him. Link 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))
### 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 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()
### 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")
+6
source to share