Skip to content

Instantly share code, notes, and snippets.

@loiyumba
Created March 15, 2016 16:31
Show Gist options
  • Save loiyumba/a9f4abf4823947db3437 to your computer and use it in GitHub Desktop.
Save loiyumba/a9f4abf4823947db3437 to your computer and use it in GitHub Desktop.
### Religious Population Map of India and Northeast India
# Load the the necessary packages
require(choroplethrAdmin1)
require(choroplethr)
# Set working directory
setwd("..\\Data\\census")
# Load the data
rel_pop <- read.csv("pop_rel_com_2011.csv")
# Drop not-require observations and variables
rel_pop <- rel_pop[, 6:34]
rel_pop <- rel_pop[, c(1:2,6,9,12,15,18, 21,24, 27)]
# Renaming variables
names(rel_pop) <- c("State", "Type" ,"Hindu", "Muslim", "Christian", "Sikh", "Buddhist", "Jain", "Other", "Not_Stated")
rel_pop <- rel_pop[10:114, ]
rel_pop <- subset(rel_pop, Type == "Total")
rel_pop$State <- substr(rel_pop$State, 8, 30)
# Load india state info
india <- get_admin1_regions("india")
india$no <- 1:35 # assign no for merging
# assign matching state with exact no for merging
rel_pop$no <- 0
india[7, 1] <- 1
rel_pop[28, 11] <- 2
rel_pop[12, 11] <- 3
rel_pop[18, 11] <- 4
rel_pop[10, 11] <- 5
rel_pop[22, 11] <- 6
rel_pop[30, 11] <- 7
rel_pop[24, 11] <- 8
rel_pop[6, 11] <- 9
rel_pop[2, 11] <- 10
rel_pop[1, 11] <- 11
rel_pop[20, 11] <- 12
rel_pop[29, 11] <- 13
rel_pop[32, 11] <- 14
rel_pop[23, 11] <- 15
rel_pop[27, 11] <- 16
rel_pop[14, 11] <- 17
rel_pop[17, 11] <- 18
rel_pop[15, 11] <- 19
rel_pop[13, 11] <- 20
rel_pop[21, 11] <- 21
rel_pop[3, 11] <- 22
rel_pop[8, 11] <- 23
rel_pop[11, 11] <- 24
rel_pop[33, 11] <- 25
rel_pop[16, 11] <- 26
rel_pop[9, 11] <- 27
rel_pop[5, 11] <- 28
rel_pop[19, 11] <- 29
rel_pop[35, 11] <- 30
rel_pop[4, 11] <- 31
rel_pop[26, 11] <- 32
rel_pop[25, 11] <- 33
rel_pop[31, 11] <- 34
rel_pop[34, 11] <- 35
# Merge the datasets
ind_pop <- merge(india, rel_pop, by = "no")
write.csv(ind_pop, "merged_data.csv", row.names = FALSE) # save the data
# Filter northeast state to zoom
ne_india <- c("state of arunachal pradesh",
"state of assam",
"state of manipur",
"state of meghalaya",
"state of mizoram",
"state of nagaland",
"state of sikkim",
"state of tripura")
# Hindu Population
hindu <- ind_pop[, c(2:3, 6)]
names(hindu)[3] <- "value"
hindu$value <- as.numeric(as.character(hindu$value))
# Draw india map for hindu population
admin1_choropleth(country.name = "india",
df = hindu,
title = "Hindu Population in India\n As per 2011 Census",
legend = "Population",
num_colors = 7)
# Draw northeast india map for hindu population
admin1_choropleth(country.name = "india",
df = hindu,
title = "Hindu Population in India\n As per 2011 Census",
legend = "Population",
num_colors = 8,
zoom = zoom = ne_india,
reference_map = TRUE) # this adds google map layer
# Reload the merged data (on day 2)
pop <- read.csv("merged_data.csv", stringsAsFactors = FALSE)
# Muslim Population
muslim <- pop[, c(2:3, 7)]
names(muslim)[3] <- "value"
muslim$value <- as.numeric(muslim$value)
# Draw india map for muslim population
admin1_choropleth(country.name = "india",
df = muslim,
title = "Muslim Population in India\n As per 2011 Census",
legend = "Population",
num_colors = 7)
# Draw northeast india map for muslim population
admin1_choropleth(country.name = "india",
df = muslim,
title = "Muslim Population in Northeast India\n As per 2011 Census",
legend = "Population",
num_colors = 8,
zoom = ne_india)
# reference_map = TRUE) # this adds google map layer
# Christian Population
christian <- pop[, c(2:3, 8)]
names(christian)[3] <- "value"
# Draw india map for christian population
admin1_choropleth(country.name = "india",
df = christian,
title = "Christian Population in India\n As per 2011 Census",
legend = "Population",
num_colors = 7)
# Draw northeast india map for christian population
admin1_choropleth(country.name = "india",
df = christian,
title = "Christian Population in Northeast India\n As per 2011 Census",
legend = "Population",
num_colors = 8,
zoom = ne_india)
# reference_map = TRUE) # this adds google map layer
# Sikh Population
sikh <- pop[, c(2:3, 9)]
names(sikh)[3] <- "value"
# Draw india map for sikh population
admin1_choropleth(country.name = "india",
df = sikh,
title = "Sikh Population in India\n As per 2011 Census",
legend = "Population",
num_colors = 7)
# Draw northeast india map for sikh population
admin1_choropleth(country.name = "india",
df = sikh,
title = "Sikh Population in Northeast India\n As per 2011 Census",
legend = "Population",
num_colors = 8,
zoom = ne_india)
# Buddhist Population
buddhist <- pop[, c(2:3, 10)]
names(buddhist)[3] <- "value"
# Draw india map for buddhist population
admin1_choropleth(country.name = "india",
df = buddhist,
title = "Buddhist Population in India\n As per 2011 Census",
legend = "Population",
num_colors = 7)
# Draw northeast india map for buddhist population
admin1_choropleth(country.name = "india",
df = buddhist,
title = "Buddhist Population in Northeast India\n As per 2011 Census",
legend = "Population",
num_colors = 8,
zoom = ne_india)
# Jain Population
jain <- pop[, c(2:3, 11)]
names(jain)[3] <- "value"
# Draw india map for jain population
admin1_choropleth(country.name = "india",
df = jain,
title = "Jain Population in India\n As per 2011 Census",
legend = "Population",
num_colors = 7)
# Draw northeast india map for jain population
admin1_choropleth(country.name = "india",
df = jain,
title = "Jain Population in Northeast India\n As per 2011 Census",
legend = "Population",
num_colors = 8,
zoom = ne_india)
# Other Population
other <- pop[, c(2:3, 12)]
names(other)[3] <- "value"
# Draw india map for other believer population
admin1_choropleth(country.name = "india",
df = other,
title = "Other Believer Population in India\n As per 2011 Census",
legend = "Population",
num_colors = 7)
# Draw northeast india map for other believer population
admin1_choropleth(country.name = "india",
df = other,
title = "Other Believer Population in Northeast India\n As per 2011 Census",
legend = "Population",
num_colors = 8,
zoom = ne_india,
reference_map = TRUE)
# Not Stated Population
ns <- pop[, c(2:3, 13)]
names(ns)[3] <- "value"
# Draw india map for not stated religion population
admin1_choropleth(country.name = "india",
df = ns,
title = "Religion Not Stated Population in India\n As per 2011 Census",
legend = "Population",
num_colors = 7,
reference_map = TRUE)
# Draw northeast india map for not stated religion population
admin1_choropleth(country.name = "india",
df = ns,
title = "Religion Not Stated Population in Northeast India\n As per 2011 Census",
legend = "Population",
num_colors = 8,
zoom = ne_india)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment