Skip to content

Instantly share code, notes, and snippets.

@meysubb
Created May 30, 2017 05:22
Show Gist options
  • Save meysubb/1ce36e2d2de3e2014d16e9603210ae16 to your computer and use it in GitHub Desktop.
Save meysubb/1ce36e2d2de3e2014d16e9603210ae16 to your computer and use it in GitHub Desktop.
library(geofacet)
library(ggplot2)
library(dplyr)
library(scales)
library(tibble)
library(rvest)
library(reshape2)
### Get India Population by State/Union
#url <- "https://en.wikipedia.org/wiki/List_of_states_and_union_territories_of_India_by_population"
### Create India grid
## create the AUS grid
india_grid1 <- tribble(~code, ~row, ~col, ~name,
"JK", 1, 2, "Jammu and Kashmir",
"PB", 2, 2, "Punjab",
"HP", 2, 3, "Himachal Pradesh",
"HR", 3, 2, "Haryana",
"UK", 3, 3, "Uttarakhand",
"AR", 3, 8, "Arunachal Pradesh",
"RJ", 4, 1, "Rajasthan",
"UP", 4, 3, "Uttar Pradesh",
"BR", 4, 4, "Bihar",
"SK", 4, 6, "Sikkim",
"AS", 4, 7, "Assam",
"NL", 4, 8, "Nagaland",
"GJ", 5, 1, "Gujarat",
"MP", 5, 2, "Madhya Pradesh",
"JH", 5, 4, "Jharkhand",
"WB", 5, 5, "West Bengal",
"ML", 5, 7, "Meghalaya",
"MN", 5, 8, "Manipur",
"MH", 6, 1, "Maharashtra",
"CG", 6, 3, "Chhattisgarh",
"OD", 6, 4, "Odisha",
"TR", 6, 7, "Tripura",
"MZ", 6, 8, "Mizoram",
"TS", 7, 2, "Telangana",
"GA", 8, 1, "Goa",
"KA", 8, 2, "Karnataka",
"AP", 8, 3, "Andhra Pradesh",
"KL", 9, 2, "Kerala",
"TN", 9, 3, "Tamil Nadu")
plot_pop <- melt(population) %>%
left_join(india_grid1, by = c("Name" = "name"))
ggplot(plot_pop, aes(variable, value/1e6, fill = `variable`)) +
geom_col() +
facet_geo(~ code, grid = india_grid1) +
scale_y_continuous(label = unit_format(unit = "M")) +
labs(title = "Indian Population Breakdown",
caption = "Data Source: Wikipedia",
x = "",
y = "Population [Millions]") +
theme_bw() +
theme(axis.text.x = element_text(angle = 40, hjust = 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment