R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

library(stringr)
url="http://www.deis.msal.gov.ar/wp-content/uploads/2020/01/DefWeb18.csv"
url1="http://www.deis.msal.gov.ar/wp-content/uploads/2019/01/DefWeb17.csv"
url2="http://www.deis.msal.gov.ar/wp-content/uploads/2018/06/DefWeb16.csv"
url3="http://www.deis.msal.gov.ar/wp-content/uploads/2018/06/DefWeb15.csv"
url4="http://www.deis.msal.gov.ar/wp-content/uploads/2018/06/DefWeb14.csv"

def2018=read.csv(url)
def2017=read.csv(url1)
def2016=read.csv(url2)
def2015=read.csv(url3)
def2014=read.csv(url4)
def2018$año<-c("2018")
def2017$año<-c("2017")
def2016$año<-c("2016")
def2015$año<-c("2015")
def2014$año<-c("2014")
deftotal <- do.call("rbind", list(def2018, def2017, def2016, def2015,def2014))
head(deftotal)
##   PROVRES SEXO CAUSA MAT       GRUPEDAD CUENTA  año
## 1       2    1   A09         12_55 a 59      1 2018
## 2       2    1   A09     17_80 y m\xe1s      3 2018
## 3       2    1   A16         15_70 a 74      3 2018
## 4       2    1   A19         07_30 a 34      1 2018
## 5       2    1   A41         05_20 a 24      1 2018
## 6       2    1   A41         06_25 a 29      3 2018
deftotal[deftotal$SEXO == 1, "SEXO"] <- "Masculino"
deftotal[deftotal$SEXO == 2, "SEXO"] <- "Femenino"
deftotal[deftotal$SEXO == 9, "SEXO"] <- "Sin especificar"
deftotal[deftotal$PROVRES == 2, "PROVRES"] <- "CABA"
deftotal[deftotal$PROVRES == 6, "PROVRES"] <- "Buenos Aires"
deftotal[deftotal$PROVRES == 10, "PROVRES"] <- "Catamarca"
deftotal[deftotal$PROVRES == 14, "PROVRES"] <- "Cordoba"
deftotal[deftotal$PROVRES == 18, "PROVRES"] <- "Corrientes"
deftotal[deftotal$PROVRES == 22, "PROVRES"] <- "Chaco"
deftotal[deftotal$PROVRES == 26, "PROVRES"] <- "Chubut"
deftotal[deftotal$PROVRES == 30, "PROVRES"] <- "Entre Rios"
deftotal[deftotal$PROVRES == 34, "PROVRES"] <- "Formosa"
deftotal[deftotal$PROVRES == 38, "PROVRES"] <- "Jujuy"
deftotal[deftotal$PROVRES == 42, "PROVRES"] <- "La Pampa"
deftotal[deftotal$PROVRES == 46, "PROVRES"] <- "La Rioja"
deftotal[deftotal$PROVRES == 50, "PROVRES"] <- "Mendoza"
deftotal[deftotal$PROVRES == 54, "PROVRES"] <- "Misiones"
deftotal[deftotal$PROVRES == 58, "PROVRES"] <- "Neuquen"
deftotal[deftotal$PROVRES == 62, "PROVRES"] <- "Rio Negro"
deftotal[deftotal$PROVRES == 66, "PROVRES"] <- "Salta"
deftotal[deftotal$PROVRES == 70, "PROVRES"] <- "San Juan"
deftotal[deftotal$PROVRES == 74, "PROVRES"] <- "San Luis"
deftotal[deftotal$PROVRES == 78, "PROVRES"] <- "Santa Cruz"
deftotal[deftotal$PROVRES == 82, "PROVRES"] <- "Santa Fe"
deftotal[deftotal$PROVRES == 86, "PROVRES"] <- "Santiago del Estero"
deftotal[deftotal$PROVRES == 90, "PROVRES"] <- "Tucuman"
deftotal[deftotal$PROVRES == 94, "PROVRES"] <- "Tierra del Fuego"
deftotal[deftotal$PROVRES == 98, "PROVRES"] <- "Otro Pais"
deftotal[deftotal$PROVRES == 99, "PROVRES"] <- "Lugar no especificado"

library("xlsx")
codmuertes<-read.xlsx("codmuertes.xlsx",sheetIndex = 1)
names(deftotal)[names(deftotal) == "CAUSA"] <- "CODIGO"
deffinal<-merge(deftotal,codmuertes, by="CODIGO")
names(deffinal)[names(deffinal) == "VALOR"] <- "CAUSA DE MUERTE"
names(deffinal)[names(deffinal) == "año"] <- "AÑO"
library(utils)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
cuenta<-deftotal %>%
  group_by(SEXO)%>%
  summarise(cantidad=sum(CUENTA))

ggplot(cuenta, aes(x="", y=cantidad, fill=SEXO)) +
  geom_bar(stat="identity", width=45) +
  coord_polar("y", start=0)

cuenta2= deftotal %>%
  group_by(año)%>%
  summarise(cantidad=sum(CUENTA))
ggplot(cuenta2, aes(x=año, y=cantidad))+
  geom_histogram(stat="identity", fill="darkcyan")+
  ggtitle("Grafico de defunciones por año")+
  xlab("Año")+
  ylab("Cantidad")+
  ylim(0,390000)+
  geom_text(aes(label=cantidad),size=3,col="black", vjust=-1)
## Warning: Ignoring unknown parameters: binwidth, bins, pad

counts= deftotal %>%
  group_by(año, SEXO)%>%
  summarise(cantidad=sum(CUENTA))

ggplot(counts, aes(fill=SEXO, y=cantidad, x=año))+
  geom_bar(position="dodge", stat="identity")+
  ggtitle("Cantidad de defunciones por año y sexo")

drt<-deftotal %>%
  group_by(PROVRES)%>%
  summarise(cantidad=sum(CUENTA))%>%
  arrange(-cantidad)
  
library(tidyverse)
## ── Attaching packages ──────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ tibble  2.1.3     ✓ purrr   0.3.3
## ✓ tidyr   1.0.2     ✓ forcats 0.5.0
## ✓ readr   1.3.1
## ── Conflicts ─────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
ggplot(drt, aes(x=fct_reorder(`PROVRES`, cantidad, .desc=FALSE), y=cantidad, label=cantidad)) + 
  geom_point(stat='identity', fill="white", size=3)  +
  geom_segment(aes(y = 0, 
                   x = `PROVRES`, 
                   yend = cantidad, 
                   xend = PROVRES), 
               color = "black") +
  geom_text(color="black", size=2.5, hjust=-0.3) +
  labs(title="Cantidad de Defunciones por Provincia",
       subtitle="Cantidades tomadas de datos del 2014 al 2018"
  ) + 
  ylim(0, 700000) +
  coord_flip()

causas=deffinal %>%
  group_by(`CAUSA DE MUERTE`) %>%
  summarise(cantidad=sum(CUENTA)) %>%
  arrange(-cantidad)


topcausas=filter(causas, `cantidad`>19390)
ggplot(topcausas, aes(x=cantidad, y=fct_reorder(`CAUSA DE MUERTE`, cantidad, .desc=FALSE), label=cantidad))+
         geom_point(stat='identity', fill="white", size=3)  +
         geom_segment(aes(xend=cantidad, x=0,yend=`CAUSA DE MUERTE`),color = "black") +
         geom_text(color="black", size=2.5, hjust=-0.3)+
  xlab("Cantidad")+
  ylab("Causa")+
  ggtitle("TOP 20 causas por cantidad de defunciones")

# TABLA PARA HACER MAPA POR PROVINCIA
filtro=filter(deftotal, SEXO=="Masculino" | SEXO=="Femenino")
prov=filtro %>%
  group_by(PROVRES, SEXO)%>%
  summarise(cantidad=sum(CUENTA))%>%
  arrange(-cantidad)


# MAPA HECHO CON JSBIN https://output.jsbin.com/mikatawiyu 
#COLOR ROSA: GENERO FEMENINO CON MAS DEFUNCIONES EN ESA PROVINCIA
#COLOR CELESTE: GENERO MASCULINO CON MAS DEFUNCIONES EN ESA PROVINCIA

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.