Skip to content

Instantly share code, notes, and snippets.

@sainathadapa
Last active March 18, 2017 04:38
Show Gist options
  • Save sainathadapa/ff621ec86f464538119200b30eedba36 to your computer and use it in GitHub Desktop.
Save sainathadapa/ff621ec86f464538119200b30eedba36 to your computer and use it in GitHub Desktop.
import-export-states-sankey
library(networkD3)
library(magrittr)
library(dplyr)
library(readr)
library(tidyr)
tmp <- read_csv('sankey-data.csv', skip = 2)
tmp <- tmp[-(21:22),]
names(tmp) <- c("X1", "Maharashtra", "Gujarat", "Karnataka", "Andhra Pradesh",
"Uttarakhand", "Rajasthan", "Madhya Pradesh", "West Bengal", "Tamil Nadu",
"Kerala", "Odisha", "Uttar Pradesh", "Delhi", "Chattisgarh",
"Himachal Pradesh", "Haryana", "Bihar", "Jharkhand", "Goa", "Assam")
tmp$X1 <- c("Maharashtra", "Gujarat", "Tamil Nadu", "Haryana", "Karnataka",
"Andhra Pradesh", "Uttar Pradesh", "Rajasthan", "Delhi", "West Bengal",
"Madhya Pradesh", "Uttarakhand", "Odisha", "Chattisgarh", "Jharkhand",
"Himachal Pradesh", "Kerala", "Goa", "Assam", "Bihar")
tmp <- tmp[, c("X1", "Maharashtra", "Gujarat", "Tamil Nadu", "Haryana", "Karnataka",
"Andhra Pradesh", "Uttar Pradesh", "Rajasthan", "Delhi", "West Bengal",
"Madhya Pradesh", "Uttarakhand", "Odisha", "Chattisgarh", "Jharkhand",
"Himachal Pradesh", "Kerala", "Goa", "Assam", "Bihar")]
tmp <- tmp %>%
gather(key = 'import_state', value = 'value', -X1) %>%
rename(export_state = X1) %>%
mutate(value = as.numeric(value)) %>%
filter(!is.na(value))
trade_surplus <- bind_rows(
tmp %>%
group_by(export_state) %>%
summarise(value = sum(value)) %>%
ungroup %>%
rename(state = export_state),
tmp %>%
group_by(import_state) %>%
summarise(value = -sum(value)) %>%
ungroup %>%
rename(state = import_state)
) %>%
group_by(state) %>%
summarise(value = sum(value)) %>%
ungroup
export_map <- tmp$export_state %>% unique %>% seq_along() - 1
names(export_map) <- tmp$export_state %>% unique %>% sort
import_map <- tmp$import_state %>% unique %>% seq_along() + 19
names(import_map) <- tmp$import_state %>% unique %>% sort
tmp2 <- list(nodes = data.frame(name = c(
tmp$export_state %>% unique %>% sort,
tmp$import_state %>% unique %>% sort
)) %>% mutate(name = as.character(name))
)
tmp2$links <- tmp %>%
transmute(
source = export_map[export_state] %>% unname,
target = import_map[import_state] %>% unname,
value = as.numeric(value),
export_state = export_state
) %>% data.frame
gg <- sankeyNetwork(Links = tmp2$links, Nodes = tmp2$nodes, Source = "source",
Target = "target", Value = "value", NodeID = "name",
units = "", fontSize = 12, nodeWidth = 30, iterations = 100,
LinkGroup = "export_state")
gg
saveNetwork(gg, 'tmp.html', selfcontained = TRUE)
Table 1: Arms-length trade flows between states (C-forms, FY2015-16 in Rupees Crores)35
Importing State
Maharashtra Gujarat Karnataka PArnaddehsrah Ukthtaanrad- Rajasthan PMraaddhesyha BWenegstal TNaamdiul Kerala Odisha PrUatdtearsh Delhi tCishghaarth- HPirmadaecshhal Haryana Bihar Jharkhand Goa Assam
Maharashtra 85679 40253 30734 22679 10215 23634 11409 18793 12336 5438 4380 6583 7783 7137 4006 3629 2957 7038 449
Gujarat 82044 12882 19679 11717 35627 22534 9664 11643 10618 6487 6464 3871 4799 4378 8105 1536 1457 4945 279
Tamil Nadu 40113 14463 40075 26217 3846 4984 7923 6520 19856 4385 2136 2879 3146 1490 1414 1136 1991 1126 331
Haryana 25267 15327 11534 8665 14124 15965 7049 7390 4761 6764 2898 6380 9469 2374 8227 3137 1882 969 871
Karnataka 32519 9828 20777 3430 3465 8229 5686 19300 13589 3332 2218 2117 2042 1158 1782 967 1535 3403 223
Andhra Pradesh 25071 6345 18941 2092 9769 4514 4664 10614 5291 6901 1728 1138 2897 1794 692 1187 726 1276 161
Uttar Pradesh 10849 8521 4037 3225 26900 5569 5983 4133 1859 1514 1450 5848 1385 3488 4873 2141 968 271 162
Rajasthan 11307 33743 3501 3644 7714 4868 5058 2231 1778 1531 3149 3368 1014 2113 2776 2020 1102 308 231
Delhi 8203 9898 4519 2642 16912 7089 2684 2885 1628 2195 1377 8791 1511 6642 6729 1265 416 216 195
West Bengal 9716 6667 2995 4550 1783 2181 4066 2194 1219 8442 1516 1399 4929 807 911 7006 6632 129 951
Madhya Pradesh 11181 6843 2267 3252 2650 7227 8540 2201 697 1288 3006 2086 3101 1008 726 1047 724 1057 55
Uttarakhand 8691 3498 4007 2890 2910 3146 3083 1497 972 1265 3191 5646 1185 2655 1786 1958 1214 188 145
Odisha 7294 2385 2426 4301 1337 940 2090 9359 575 357 933 347 6832 174 311 1574 2172 34 187
Chhattisgarh 11062 4265 2421 5556 505 2866 5923 1635 687 412 2659 743 646 330 347 342 852 120 150
Jharkhand 2092 1029 1347 1388 1750 1316 1174 10930 472 297 1796 2733 518 1657 421 1183 4670 35 48
PHraidemshachal 3295 1571 1368 1060 2410 1491 1630 919 481 309 302 1228 2388 243 1560 555 137 54 23
Kerala 2830 1041 3820 1383 261 183 326 421 3559 221 208 338 73 160 89 72 82 90 5
Goa 2756 716 1817 552 177 285 386 405 265 678 121 108 266 69 106 90 85 59 14
Assam 602 695 203 212 75 157 541 1777 33 22 169 266 188 236 21 158 608 96 0
Bihar 121 44 42 38 110 85 136 417 67 3 85 267 59 96 32 15 464 0 12
35 The total value of exports and imports of states reported in these tables are restricted to the set of states considered within the balanced dyad table. The actual value of
trade is higher because of flows from other states and union territories that have been excluded from this table for presentational convenience.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment