Plotly Map Test

9.13.2018
Deondre' Jones

  1. library(tidyverse)
  2. library(knitr)
  3. library(stringr)
  4. library(scales)
  5. library(httr)
  6. library(ggplot2)
  7. library(ggmap)
  8. library(fiftystater)
  9. library(plotly)
  10. source('https://raw.githubusercontent.com/UrbanInstitute/urban_R_theme/master/urban_theme_windows.R')
  11.  
  12. #Number of public charities per state
  13.  
  14. #Create NTEE grouping categories
  15. arts <- c("A")
  16. highered <- c("B4", "B5")
  17. othered <- c("B")
  18. envanimals <- c("C", "D")
  19. hospitals <- c('E20','E21','E22','E23','E24','F31','E30','E31','E32')
  20. otherhlth <- c("E", "F", "G", "H")
  21. humanserv <- c("I", "J", "K", "L", "M", "N", "O", "P")
  22. intl <- c("Q")
  23. pubben <- c("R", "S", "T", "U", "V", "W", "Y", "Z")
  24. relig <- c("X")
  25.  
  26. #link to NCCS Data Archive
  27. nteedoc<- GET("http://nccs-data.urban.org/data/misc/nccs.nteedocAllEins.csv")
  28.  
  29. #pull only the most important columns (EIN, , NTEECC, Nteefinal)
  30. nteedocalleins <-content(nteedoc, type = "text/csv",
  31.                          col_types=cols_only(EIN = col_character(),
  32.                                              NteeCC = col_character(),
  33.                                              NteeFinal = col_character()))
  34.  
  35. #convert variable names to upper case
  36. names(nteedocalleins) <- toupper(names(nteedocalleins))
  37.  
  38. #This function will apply the most common NTEE Grouping categories to your data.
  39. NTEEclassify <- function(dataset) {
  40.   #merge in Master NTEE look up file
  41.   dataset <- dataset %>%
  42.     left_join(nteedocalleins, by = "EIN")
  43.   #create NTEEGRP classifications
  44.   dataset$NTEEGRP <- "  "
  45.   dataset$NTEEGRP[str_sub(dataset$NTEEFINAL,1,1) %in% arts ] <- "Arts"
  46.   dataset$NTEEGRP[str_sub(dataset$NTEEFINAL,1,1) %in% othered ] <- "Education: Other"
  47.   dataset$NTEEGRP[str_sub(dataset$NTEEFINAL,1,2) %in% highered ] <- "Education: Higher"
  48.   dataset$NTEEGRP[str_sub(dataset$NTEEFINAL,1,1) %in% envanimals] <- "Environment and Animals"
  49.   dataset$NTEEGRP[str_sub(dataset$NTEEFINAL,1,1) %in% otherhlth] <- "Health Care: Other"
  50.   dataset$NTEEGRP[str_sub(dataset$NTEEFINAL,1,3) %in% hospitals] <- "Health Care: Hospitals and primary care facilities"
  51.   dataset$NTEEGRP[str_sub(dataset$NTEEFINAL,1,1) %in% humanserv] <- "Human Services"
  52.   dataset$NTEEGRP[str_sub(dataset$NTEEFINAL,1,1) %in% intl] <- "International"
  53.   dataset$NTEEGRP[str_sub(dataset$NTEEFINAL,1,1) %in% pubben] <- "Other Public and social benefit"
  54.   dataset$NTEEGRP[str_sub(dataset$NTEEFINAL,1,1) %in% relig] <- "Religion related"
  55.   dataset$NTEEGRP[is.na(dataset$NTEEFINAL)] <- "Other Public and social benefit"
  56.   return(dataset)
  57. }
  58.  
  59.  
  60. #Import reduced NCCS Core File Function
  61. prepcorepcfile <- function(corefilepath) {
  62.   output <- read_csv(corefilepath,
  63.                      col_types = cols_only(EIN = col_character(),
  64.                                            FISYR = col_integer(),
  65.                                            NAME = col_character(),
  66.                                            STATE = col_character(),
  67.                                            ADDRESS = col_character(),
  68.                                            CITY = col_character(),
  69.                                            ZIP = col_character(),
  70.                                            MSA_NECH = col_character(),
  71.                                            FIPS = col_character(),
  72.                                            PMSA = col_character(),
  73.                                            STYEAR = col_double(),
  74.                                            TAXPER = col_integer(),
  75.                                            OUTNCCS = col_character(),
  76.                                            OutNCCS = col_character(),
  77.                                            SUBSECCD = col_character(),
  78.                                            RULEDATE = col_character(),
  79.                                            FNDNCD = col_character(),
  80.                                            FRCD = col_character(),
  81.                                            TOTREV = col_double(),
  82.                                            EXPS = col_double(),
  83.                                            ASS_EOY = col_double(),
  84.                                            GRREC = col_double()
  85.  
  86.                      ))
  87.   names(output) <- toupper(names(output))
  88.   return(output)
  89. }
  90.  
  91.  
  92. #Import NCCS Core File for given year
  93. corefile <- prepcorepcfile(as.character(paste("Data/core", "2015", "pc.csv", sep="")))
  94.  
  95. #Add NTEE Classifications to the Core File
  96. corefile <- NTEEclassify(corefile)
  97.  
  98. #Filter out of scope organizations 
  99. corefile <- corefile %>%
  100.   filter((OUTNCCS != "OUT")) %>%
  101.   filter((FNDNCD != "02" & FNDNCD!= "03" & FNDNCD != "04"))
  102.  
  103.  
  104. #Create table counting number of charities per state
  105. perstate <- data.frame(table(corefile$STATE))
  106.  
  107. #Rename columns
  108. colnames(perstate) <- c('STATE', 'freq')
  109.  
  110. #Create column of full state names + DC
  111. perstate$statenames <- state.name[match(perstate$STATE, state.abb)]
  112. perstate$statenames <- perstate$statenames %>%
  113.   replace_na("District of Columbia")
  114.  
  115. #Create column for hover features 
  116. perstate$hover <- with(perstate, paste(statenames,'<br>', "Number of Public Charities", '=', prettyNum(freq, big.mark = ",")))
  117.  
  118. #Plot map
  119. urban_colors <- c("#cfe8f3", "#a2d4ec", "#46abdb", "#12719e", "#062635")
  120. l <- list(color = '#ffffff', width = 2)
  121. t <- list( size = 12, color = "#000000")
  122. g <- list(scope = 'usa', projection = list(type = 'albers usa'))
  123. p <- plot_geo(perstate, locationmode = 'USA-states') %>%
  124.   add_trace(
  125.     z = ~freq, text = ~hover, locations = ~STATE,
  126.     color = ~freq, colors = urban_colors, marker = list(line = l)
  127.   ) %>%
  128.   colorbar(title = "Number of Nonprofits", font = t, outlinecolor = "#ffffff") %>%
  129.   layout(
  130.     title = 'Number of Nonprofits per State 2015', 
  131.     font = "Lato",
  132.     geo = g,
  133.     width = 900, height = 600
  134.   )
  135.  
  136. p

{"x":{"visdat":{"27a810dc52be":["function () ","plotlyVisDat"]},"cur_data":"27a810dc52be","attrs":{"27a810dc52be":{"locationmode":"USA-states","alpha_stroke":1,"sizes":[10,100],"spans":[1,20],"z":{},"text":{},"locations":{},"color":{},"colors":["#cfe8f3","#a2d4ec","#46abdb","#12719e","#062635"],"marker":{"line":{"color":"#ffffff","width":2}},"inherit":true}},"layout":{"margin":{"b":40,"l":60,"t":25,"r":10},"mapType":"geo","scene":{"zaxis":{"title":"freq"}},"geo":{"domain":{"x":[0,1],"y":[0,1]},"scope":"usa","projection":{"type":"albers usa"}},"hovermode":"closest","showlegend":false,"legend":{"yanchor":"top","y":0.5},"title":"Number of Nonprofits per State 2015","font":"Lato","width":900,"height":600},"source":"A","config":{"modeBarButtonsToAdd":[{"name":"Collaborate","icon":{"width":1000,"ascent":500,"descent":-50,"path":"M487 375c7-10 9-23 5-36l-79-259c-3-12-11-23-22-31-11-8-22-12-35-12l-263 0c-15 0-29 5-43 15-13 10-23 23-28 37-5 13-5 25-1 37 0 0 0 3 1 7 1 5 1 8 1 11 0 2 0 4-1 6 0 3-1 5-1 6 1 2 2 4 3 6 1 2 2 4 4 6 2 3 4 5 5 7 5 7 9 16 13 26 4 10 7 19 9 26 0 2 0 5 0 9-1 4-1 6 0 8 0 2 2 5 4 8 3 3 5 5 5 7 4 6 8 15 12 26 4 11 7 19 7 26 1 1 0 4 0 9-1 4-1 7 0 8 1 2 3 5 6 8 4 4 6 6 6 7 4 5 8 13 13 24 4 11 7 20 7 28 1 1 0 4 0 7-1 3-1 6-1 7 0 2 1 4 3 6 1 1 3 4 5 6 2 3 3 5 5 6 1 2 3 5 4 9 2 3 3 7 5 10 1 3 2 6 4 10 2 4 4 7 6 9 2 3 4 5 7 7 3 2 7 3 11 3 3 0 8 0 13-1l0-1c7 2 12 2 14 2l218 0c14 0 25-5 32-16 8-10 10-23 6-37l-79-259c-7-22-13-37-20-43-7-7-19-10-37-10l-248 0c-5 0-9-2-11-5-2-3-2-7 0-12 4-13 18-20 41-20l264 0c5 0 10 2 16 5 5 3 8 6 10 11l85 282c2 5 2 10 2 17 7-3 13-7 17-13z m-304 0c-1-3-1-5 0-7 1-1 3-2 6-2l174 0c2 0 4 1 7 2 2 2 4 4 5 7l6 18c0 3 0 5-1 7-1 1-3 2-6 2l-173 0c-3 0-5-1-8-2-2-2-4-4-4-7z m-24-73c-1-3-1-5 0-7 2-2 3-2 6-2l174 0c2 0 5 0 7 2 3 2 4 4 5 7l6 18c1 2 0 5-1 6-1 2-3 3-5 3l-174 0c-3 0-5-1-7-3-3-1-4-4-5-6z"},"click":"function(gd) { \n // is this being viewed in RStudio?\n if (location.search == '?viewer_pane=1') {\n alert('To learn about plotly for collaboration, visit:\\n https://cpsievert.github.io/plotly_book/plot-ly-for-collaboration.html');\n } else {\n window.open('https://cpsievert.github.io/plotly_book/plot-ly-for-collaboration.html', '_blank');\n }\n }"}],"cloud":false},"data":[{"colorbar":{"title":"Number of Nonprofits","ticklen":2,"len":0.5,"lenmode":"fraction","y":1,"yanchor":"top","font":{"size":12,"color":"#000000"},"outlinecolor":"#ffffff"},"colorscale":[["0","rgba(207,232,243,1)"],["0.00553831781937077","rgba(206,232,243,1)"],["0.00933506822807193","rgba(205,231,243,1)"],["0.0141127014312882","rgba(205,231,243,1)"],["0.0196310012344443","rgba(204,230,242,1)"],["0.024118373202549","rgba(203,230,242,1)"],["0.0268141327194475","rgba(202,230,242,1)"],["0.0310245887965836","rgba(202,230,242,1)"],["0.0351249457845394","rgba(201,229,242,1)"],["0.0524972475227705","rgba(198,228,242,1)"],["0.068781903713342","rgba(195,226,241,1)"],["0.0729306375738164","rgba(194,226,241,1)"],["0.0780102091882694","rgba(193,226,241,1)"],["0.0999349414473026","rgba(189,224,240,1)"],["0.120835418543356","rgba(186,222,240,1)"],["0.134531077970173","rgba(183,221,239,1)"],["0.149341073632936","rgba(181,220,239,1)"],["0.163357021319187","rgba(178,219,238,1)"],["0.185146631968772","rgba(174,217,238,1)"],["0.214367931138024","rgba(169,215,237,1)"],["0.228152003469789","rgba(166,214,237,1)"],["0.291031928735862","rgba(149,205,233,1)"],["0.35463250258566","rgba(128,195,229,1)"],["0.499983318319821","rgba(70,171,219,1)"],["1","rgba(6,38,53,1)"]],"showscale":true,"locationmode":"USA-states","z":[1584,5055,3374,6131,51113,9013,6362,4627,1432,20895,11067,2188,4988,1848,16552,8417,3916,4810,4705,14657,9261,2752,11669,9399,7699,2576,2352,12068,1318,2987,2646,12799,2764,2114,32753,16043,4429,7106,19338,1826,5562,1464,7636,26611,2419,12008,1908,9747,8421,2378,1158],"text":["Alaska
Number of Public Charities = 1,584","Alabama
Number of Public Charities = 5,055","Arkansas
Number of Public Charities = 3,374","Arizona
Number of Public Charities = 6,131","California
Number of Public Charities = 51,113","Colorado
Number of Public Charities = 9,013","Connecticut
Number of Public Charities = 6,362","District of Columbia
Number of Public Charities = 4,627","Delaware
Number of Public Charities = 1,432","Florida
Number of Public Charities = 20,895","Georgia
Number of Public Charities = 11,067","Hawaii
Number of Public Charities = 2,188","Iowa
Number of Public Charities = 4,988","Idaho
Number of Public Charities = 1,848","Illinois
Number of Public Charities = 16,552","Indiana
Number of Public Charities = 8,417","Kansas
Number of Public Charities = 3,916","Kentucky
Number of Public Charities = 4,810","Louisiana
Number of Public Charities = 4,705","Massachusetts
Number of Public Charities = 14,657","Maryland
Number of Public Charities = 9,261","Maine
Number of Public Charities = 2,752","Michigan
Number of Public Charities = 11,669","Minnesota
Number of Public Charities = 9,399","Missouri
Number of Public Charities = 7,699","Mississippi
Number of Public Charities = 2,576","Montana
Number of Public Charities = 2,352","North Carolina
Number of Public Charities = 12,068","North Dakota
Number of Public Charities = 1,318","Nebraska
Number of Public Charities = 2,987","New Hampshire
Number of Public Charities = 2,646","New Jersey
Number of Public Charities = 12,799","New Mexico
Number of Public Charities = 2,764","Nevada
Number of Public Charities = 2,114","New York
Number of Public Charities = 32,753","Ohio
Number of Public Charities = 16,043","Oklahoma
Number of Public Charities = 4,429","Oregon
Number of Public Charities = 7,106","Pennsylvania
Number of Public Charities = 19,338","Rhode Island
Number of Public Charities = 1,826","South Carolina
Number of Public Charities = 5,562","South Dakota
Number of Public Charities = 1,464","Tennessee
Number of Public Charities = 7,636","Texas
Number of Public Charities = 26,611","Utah
Number of Public Charities = 2,419","Virginia
Number of Public Charities = 12,008","Vermont
Number of Public Charities = 1,908","Washington
Number of Public Charities = 9,747","Wisconsin
Number of Public Charities = 8,421","West Virginia
Number of Public Charities = 2,378","Wyoming
Number of Public Charities = 1,158"],"locations":["AK","AL","AR","AZ","CA","CO","CT","DC","DE","FL","GA","HI","IA","ID","IL","IN","KS","KY","LA","MA","MD","ME","MI","MN","MO","MS","MT","NC","ND","NE","NH","NJ","NM","NV","NY","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VA","VT","WA","WI","WV","WY"],"marker":{"line":{"colorbar":{"title":"","ticklen":2},"cmin":1158,"cmax":51113,"colorscale":[["0","rgba(207,232,243,1)"],["0.00553831781937077","rgba(206,232,243,1)"],["0.00933506822807193","rgba(205,231,243,1)"],["0.0141127014312882","rgba(205,231,243,1)"],["0.0196310012344443","rgba(204,230,242,1)"],["0.024118373202549","rgba(203,230,242,1)"],["0.0268141327194475","rgba(202,230,242,1)"],["0.0310245887965836","rgba(202,230,242,1)"],["0.0351249457845394","rgba(201,229,242,1)"],["0.0524972475227705","rgba(198,228,242,1)"],["0.068781903713342","rgba(195,226,241,1)"],["0.0729306375738164","rgba(194,226,241,1)"],["0.0780102091882694","rgba(193,226,241,1)"],["0.0999349414473026","rgba(189,224,240,1)"],["0.120835418543356","rgba(186,222,240,1)"],["0.134531077970173","rgba(183,221,239,1)"],["0.149341073632936","rgba(181,220,239,1)"],["0.163357021319187","rgba(178,219,238,1)"],["0.185146631968772","rgba(174,217,238,1)"],["0.214367931138024","rgba(169,215,237,1)"],["0.228152003469789","rgba(166,214,237,1)"],["0.291031928735862","rgba(149,205,233,1)"],["0.35463250258566","rgba(128,195,229,1)"],["0.499983318319821","rgba(70,171,219,1)"],["1","rgba(6,38,53,1)"]],"showscale":false,"color":"#ffffff","width":2}},"type":"choropleth","geo":"geo","frame":null}],"highlight":{"on":"plotly_click","persistent":false,"dynamic":false,"selectize":false,"opacityDim":0.2,"selected":{"opacity":1},"debounce":0},"base_url":"https://plot.ly"},"evals":["config.modeBarButtonsToAdd.0.click"],"jsHooks":[]}