library(tidyverse)
## ── Attaching packages ────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.2.1     ✔ purrr   0.3.3
## ✔ tibble  2.1.3     ✔ dplyr   0.8.3
## ✔ tidyr   1.0.0     ✔ stringr 1.4.0
## ✔ readr   1.3.1     ✔ forcats 0.4.0
## ── Conflicts ───────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(tidycensus)
library(mapview)
census_api_key("b5e3d2da685c019db5e7c341c3949706ef5df120",install = TRUE, overwrite = TRUE)
## Your original .Renviron will be backed up and stored in your R HOME directory if needed.
## Your API key has been stored in your .Renviron and can be accessed by Sys.getenv("CENSUS_API_KEY"). 
## To use now, restart R or run `readRenviron("~/.Renviron")`
## [1] "b5e3d2da685c019db5e7c341c3949706ef5df120"
readRenviron("~/.Renviron")
NEWJERSEY <- get_acs(geography = "state", year = 2017, 
      variables = c(Argentina = "B05006_149", Uruguay = "B05006_157", Chile = "B05006_152", Brazil = "B05006_151", Bolivia = "B05006_150", Colombia = "B05006_153", Ecuador = "B05006_154", Peru = "B05006_156", Venezuela = "B05006_158"), state = 34)
## Getting data from the 2013-2017 5-year ACS
ggplot(NEWJERSEY, aes(x = reorder(variable, -estimate), y =estimate / 1000, color = variable, fill = variable))+
  theme(axis.text.x=element_text(angle=90,hjust=1)) +
  geom_col()+
  ggtitle("PLACE OF BIRTH FOR THE FOREIGN-BORN POPULATION IN NJ")+
  xlab("South American Spanish-Speaking Countries")+
  ylab("Population (In Thousands)")

US <- get_acs(geography = "us", year = 2017,
      variables = c(Argentina = "B05006_149", Uruguay = "B05006_157", Chile = "B05006_152", Brazil = "B05006_151", Bolivia = "B05006_150", Colombia = "B05006_153", Ecuador = "B05006_154", Peru = "B05006_156", Venezuela = "B05006_158"))
## Getting data from the 2013-2017 5-year ACS
ggplot(US, aes(x = reorder(variable, -estimate), y =estimate / 1000, color = variable, fill = variable))+
  theme(axis.text.x=element_text(angle=90,hjust=1)) +
  geom_col()+
  ggtitle("PLACE OF BIRTH FOR THE FOREIGN-BORN POPULATION IN THE UNITED STATES")+
  xlab("South American Spanish-Speaking Countries")+
  ylab("Population (In Thousands)")

test <- get_acs(geography = "state", year = 2017,state = c("new jersey", "texas","california","florida", "new york"),variables = c(very_well = "B06007_036", not_very_well = "B06007_037"), summary_var = "B06007_035")
## Getting data from the 2013-2017 5-year ACS
head(test)
test %>%
  mutate(pctvs = 100 * (estimate / summary_est)) %>%
  ggplot() + geom_col(aes(x= NAME, y = pctvs, fill = variable,position="fill", stat="identity"))+
 geom_text(aes (label = round(pctvs,2),y = pctvs, x = NAME), vjust = 1.5, color = "white") +
        scale_fill_manual(values = c("#a1d99b", "#31a354")) 
## Warning: Ignoring unknown aesthetics: position, stat

# scale_y_continuous(labels = scales::percent)
englishprof <- get_acs(geography = "state", year = 2017,state = c("texas","new jersey", "new york","california","florida"),
      variables = c(very_well = "B06007_036", not_very_well = "B06007_037")) %>%
      filter(estimate > 4000)
## Getting data from the 2013-2017 5-year ACS
ggplot() + geom_bar(aes(y = (estimate/1000000), x = reorder(NAME,-estimate), fill = variable), data = englishprof, stat="identity")+
  theme(axis.text.x=element_text(angle=90,hjust=1))

#x axis represents millions
test <- get_acs(geography = "county", year = 2017,state = c("new jersey"),variables = c(very_well = "B06007_036", not_very_well = "B06007_037"), summary_var = "B06007_035", geometry = TRUE)
## Getting data from the 2013-2017 5-year ACS
## Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
## 
  |                                                                       
  |                                                                 |   0%
  |                                                                       
  |                                                                 |   1%
  |                                                                       
  |=                                                                |   1%
  |                                                                       
  |=                                                                |   2%
  |                                                                       
  |==                                                               |   2%
  |                                                                       
  |==                                                               |   3%
  |                                                                       
  |==                                                               |   4%
  |                                                                       
  |===                                                              |   4%
  |                                                                       
  |===                                                              |   5%
  |                                                                       
  |====                                                             |   5%
  |                                                                       
  |====                                                             |   6%
  |                                                                       
  |====                                                             |   7%
  |                                                                       
  |=====                                                            |   7%
  |                                                                       
  |=====                                                            |   8%
  |                                                                       
  |======                                                           |   9%
  |                                                                       
  |======                                                           |  10%
  |                                                                       
  |=======                                                          |  10%
  |                                                                       
  |=======                                                          |  11%
  |                                                                       
  |========                                                         |  12%
  |                                                                       
  |========                                                         |  13%
  |                                                                       
  |=========                                                        |  13%
  |                                                                       
  |=========                                                        |  14%
  |                                                                       
  |=========                                                        |  15%
  |                                                                       
  |==========                                                       |  15%
  |                                                                       
  |==========                                                       |  16%
  |                                                                       
  |===========                                                      |  16%
  |                                                                       
  |===========                                                      |  17%
  |                                                                       
  |===========                                                      |  18%
  |                                                                       
  |============                                                     |  18%
  |                                                                       
  |============                                                     |  19%
  |                                                                       
  |=============                                                    |  19%
  |                                                                       
  |=============                                                    |  20%
  |                                                                       
  |=============                                                    |  21%
  |                                                                       
  |==============                                                   |  21%
  |                                                                       
  |==============                                                   |  22%
  |                                                                       
  |===============                                                  |  22%
  |                                                                       
  |===============                                                  |  23%
  |                                                                       
  |===============                                                  |  24%
  |                                                                       
  |================                                                 |  24%
  |                                                                       
  |================                                                 |  25%
  |                                                                       
  |=================                                                |  25%
  |                                                                       
  |=================                                                |  26%
  |                                                                       
  |=================                                                |  27%
  |                                                                       
  |==================                                               |  27%
  |                                                                       
  |==================                                               |  28%
  |                                                                       
  |===================                                              |  29%
  |                                                                       
  |===================                                              |  30%
  |                                                                       
  |====================                                             |  30%
  |                                                                       
  |====================                                             |  31%
  |                                                                       
  |====================                                             |  32%
  |                                                                       
  |=====================                                            |  32%
  |                                                                       
  |=====================                                            |  33%
  |                                                                       
  |======================                                           |  33%
  |                                                                       
  |======================                                           |  34%
  |                                                                       
  |======================                                           |  35%
  |                                                                       
  |=======================                                          |  35%
  |                                                                       
  |=======================                                          |  36%
  |                                                                       
  |========================                                         |  36%
  |                                                                       
  |========================                                         |  37%
  |                                                                       
  |========================                                         |  38%
  |                                                                       
  |=========================                                        |  38%
  |                                                                       
  |=========================                                        |  39%
  |                                                                       
  |==========================                                       |  39%
  |                                                                       
  |==========================                                       |  40%
  |                                                                       
  |==========================                                       |  41%
  |                                                                       
  |===========================                                      |  41%
  |                                                                       
  |===========================                                      |  42%
  |                                                                       
  |============================                                     |  42%
  |                                                                       
  |============================                                     |  43%
  |                                                                       
  |============================                                     |  44%
  |                                                                       
  |=============================                                    |  44%
  |                                                                       
  |=============================                                    |  45%
  |                                                                       
  |==============================                                   |  45%
  |                                                                       
  |==============================                                   |  46%
  |                                                                       
  |==============================                                   |  47%
  |                                                                       
  |===============================                                  |  47%
  |                                                                       
  |===============================                                  |  48%
  |                                                                       
  |================================                                 |  49%
  |                                                                       
  |================================                                 |  50%
  |                                                                       
  |=================================                                |  50%
  |                                                                       
  |=================================                                |  51%
  |                                                                       
  |==================================                               |  52%
  |                                                                       
  |==================================                               |  53%
  |                                                                       
  |===================================                              |  53%
  |                                                                       
  |===================================                              |  54%
  |                                                                       
  |===================================                              |  55%
  |                                                                       
  |====================================                             |  55%
  |                                                                       
  |====================================                             |  56%
  |                                                                       
  |=====================================                            |  56%
  |                                                                       
  |=====================================                            |  57%
  |                                                                       
  |=====================================                            |  58%
  |                                                                       
  |======================================                           |  58%
  |                                                                       
  |======================================                           |  59%
  |                                                                       
  |=======================================                          |  59%
  |                                                                       
  |=======================================                          |  60%
  |                                                                       
  |=======================================                          |  61%
  |                                                                       
  |========================================                         |  61%
  |                                                                       
  |========================================                         |  62%
  |                                                                       
  |=========================================                        |  62%
  |                                                                       
  |=========================================                        |  63%
  |                                                                       
  |=========================================                        |  64%
  |                                                                       
  |==========================================                       |  64%
  |                                                                       
  |==========================================                       |  65%
  |                                                                       
  |===========================================                      |  65%
  |                                                                       
  |===========================================                      |  66%
  |                                                                       
  |===========================================                      |  67%
  |                                                                       
  |============================================                     |  67%
  |                                                                       
  |============================================                     |  68%
  |                                                                       
  |=============================================                    |  68%
  |                                                                       
  |=============================================                    |  69%
  |                                                                       
  |=============================================                    |  70%
  |                                                                       
  |==============================================                   |  70%
  |                                                                       
  |==============================================                   |  71%
  |                                                                       
  |==============================================                   |  72%
  |                                                                       
  |===============================================                  |  72%
  |                                                                       
  |===============================================                  |  73%
  |                                                                       
  |================================================                 |  73%
  |                                                                       
  |================================================                 |  74%
  |                                                                       
  |================================================                 |  75%
  |                                                                       
  |=================================================                |  75%
  |                                                                       
  |=================================================                |  76%
  |                                                                       
  |==================================================               |  76%
  |                                                                       
  |==================================================               |  77%
  |                                                                       
  |==================================================               |  78%
  |                                                                       
  |===================================================              |  78%
  |                                                                       
  |===================================================              |  79%
  |                                                                       
  |====================================================             |  79%
  |                                                                       
  |====================================================             |  80%
  |                                                                       
  |====================================================             |  81%
  |                                                                       
  |=====================================================            |  81%
  |                                                                       
  |=====================================================            |  82%
  |                                                                       
  |======================================================           |  82%
  |                                                                       
  |======================================================           |  83%
  |                                                                       
  |======================================================           |  84%
  |                                                                       
  |=======================================================          |  84%
  |                                                                       
  |=======================================================          |  85%
  |                                                                       
  |========================================================         |  86%
  |                                                                       
  |========================================================         |  87%
  |                                                                       
  |=========================================================        |  87%
  |                                                                       
  |=========================================================        |  88%
  |                                                                       
  |==========================================================       |  89%
  |                                                                       
  |==========================================================       |  90%
  |                                                                       
  |===========================================================      |  90%
  |                                                                       
  |===========================================================      |  91%
  |                                                                       
  |============================================================     |  92%
  |                                                                       
  |============================================================     |  93%
  |                                                                       
  |=============================================================    |  93%
  |                                                                       
  |=============================================================    |  94%
  |                                                                       
  |==============================================================   |  95%
  |                                                                       
  |==============================================================   |  96%
  |                                                                       
  |===============================================================  |  96%
  |                                                                       
  |===============================================================  |  97%
  |                                                                       
  |===============================================================  |  98%
  |                                                                       
  |================================================================ |  98%
  |                                                                       
  |================================================================ |  99%
  |                                                                       
  |=================================================================|  99%
  |                                                                       
  |=================================================================| 100%
head(test)
test %>%
  mutate(pctvs = 100 * (estimate / summary_est)) %>%
  ggplot(aes(fill = pctvs)) +
  facet_wrap(~variable) +
  geom_sf(color = NA) +
  coord_sf(crs = 26915) + 
  scale_fill_viridis_c()

B05006_157 = Estimate!!Total!!Americas!!Latin America!!South America!!Uruguay
years <- lst(2012, 2013, 2014,2015,2016,2017)

Uruguay <- map_dfr(years, ~ get_acs(geography = "us",variables = "B03001_024",year = .x,survey = "acs1"), .id = "year")
## Getting data from the 2012 1-year ACS
## The one-year ACS provides data for geographies with populations of 65,000 and greater.
## Getting data from the 2013 1-year ACS
## The one-year ACS provides data for geographies with populations of 65,000 and greater.
## Getting data from the 2014 1-year ACS
## The one-year ACS provides data for geographies with populations of 65,000 and greater.
## Getting data from the 2015 1-year ACS
## The one-year ACS provides data for geographies with populations of 65,000 and greater.
## Getting data from the 2016 1-year ACS
## The one-year ACS provides data for geographies with populations of 65,000 and greater.
## Getting data from the 2017 1-year ACS
## The one-year ACS provides data for geographies with populations of 65,000 and greater.
ggplot(Uruguay, aes(x = year, y = estimate, color = variable, fill = variable, group = 1))+
  theme_bw(base_size = 10) +
  theme(axis.text.x=element_text(angle=90,hjust=1)) +
  geom_line()

nj_pop <- 
  get_acs(geography = "county",
          variables = "B01003_001",
          state = "NJ",
          geometry = TRUE)
## Getting data from the 2013-2017 5-year ACS
## Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
mapview(nj_pop, zcol = "estimate")
fill <- c("#40b8d0", "#b2d183")

p4 <- ggplot(US, aes(y = percentage, x = year, fill = product), stat="identity")
  geom_bar() 
## geom_bar: width = NULL, na.rm = FALSE
## stat_count: width = NULL, na.rm = FALSE
## position_stack