Entregable RPubs

Enunciado

Valentina Cabrera

Crear un nuevo dataframe que sea un subconjunto del dataframe original de dfFires. El subconjunto debe contener todos los incendios del Estado de Idaho y las columnas deben ser limitadas para que sólo estén presentes las columnas YEAR_, CAUSE y TOTALACRES. Cambie el nombre de las columnas. Agrupe los datos por CAUSE y YEAR_ y luego resuma por el total de acres quemados. Trazar los resultados.

Librerías y paquetes

install.packages("dplyr")
install.packages("readr")
install.packages("ggplot2")
library(dplyr)
library(readr)
library(ggplot2)

DataFrame original

A continuación, se muestra el DataFrame original que se usará para el desarrollo de este ejercicio.

dfFires = read_csv("StudyArea.csv", col_types = cols(.default = "c"), col_names = TRUE)
knitr::kable(head(dfFires))
FID ORGANIZATI UNIT SUBUNIT SUBUNIT2 FIRENAME CAUSE YEAR_ STARTDATED CONTRDATED OUTDATED STATE STATE_FIPS TOTALACRES
0 FWS 81682 USCADBR San Diego Bay National Wildlife Refuge PUMP HOUSE Human 2001 1/1/01 0:00 1/1/01 0:00 NA California 6 0.1
1 FWS 81682 USCADBR San Diego Bay National Wildlife Refuge I5 Human 2002 5/3/02 0:00 5/3/02 0:00 NA California 6 3
2 FWS 81682 USCADBR San Diego Bay National Wildlife Refuge SOUTHBAY Human 2002 6/1/02 0:00 6/1/02 0:00 NA California 6 0.5
3 FWS 81682 USCADBR San Diego Bay National Wildlife Refuge MARINA Human 2001 7/12/01 0:00 7/12/01 0:00 NA California 6 0.1
4 FWS 81682 USCADBR San Diego Bay National Wildlife Refuge HILL Human 1994 9/13/94 0:00 9/13/94 0:00 NA California 6 1
5 FWS 81682 USCADBR San Diego Bay National Wildlife Refuge IRRIGATION Human 1994 4/22/94 0:00 4/22/94 0:00 NA California 6 0.1

Transformación de DataFrame

Ahora, procederemos a filtrar (filter()) y acotar (select()) dfFires para la creación del subconjunto solicitado. Además, cambiaremos el nombre de las columnas del nuevo dataframe.

dfIdaho = filter(dfFires, STATE == 'Idaho') #Filtrado 
dfIdaho = select(dfIdaho, YEAR_, CAUSE, TOTALACRES) #Acotado. Selección de columnas en específico
dfIdaho = select(dfIdaho, "AÑO" = "YEAR_", "CAUSA" = "CAUSE", "ACRES" = "TOTALACRES")
knitr::kable(head(dfIdaho))
AÑO CAUSA ACRES
1987 Human 5
1991 Natural 150
1991 Human 800
1990 Natural 2
1985 Human 38
1988 Human 2

Agrupar y resumir los datos

Con el nuevo dataframe creado, haremos uso de las funciones groub.by() y summarize() para agrupar los datos por AÑO y CAUSA, y resumir por el total de ACRES, respectivamente.

En este caso, dado que estamos trabajando con una gran cantidad de datos, vamos a agregar una nueva columna para indicar a qué década pertenece cada año.

dfIdaho <- mutate(dfIdaho, DECADA = ifelse(AÑO %in% 1980:1989, "1980-1989", 
                          ifelse(AÑO %in% 1990:1999, "1990-1999",
                          ifelse(AÑO %in% 2000:2009, "2000-2009", 
                          ifelse(AÑO %in% 2010:2016, "2010-2016", "-99")))))
knitr::kable(head(dfIdaho))
AÑO CAUSA ACRES DECADA
1987 Human 5 1980-1989
1991 Natural 150 1990-1999
1991 Human 800 1990-1999
1990 Natural 2 1990-1999
1985 Human 38 1980-1989
1988 Human 2 1980-1989
  • Por AÑO (décadas) y CAUSA
dfIdaho$ACRES <- as.numeric(as.character(dfIdaho$ACRES))
grpsum = dfIdaho %>%
  group_by(DECADA,CAUSA) %>%
  summarize(PROM_ACRES = mean(ACRES, na.rm = TRUE))

knitr::kable(head(grpsum))
DECADA CAUSA PROM_ACRES
1980-1989 Human 571.80455
1980-1989 Natural 517.23274
1980-1989 NA 52.58621
1990-1999 Human 236.62499
1990-1999 Natural 281.89232
1990-1999 NA 167.35833
ggplot(grpsum , aes(x = DECADA , y = PROM_ACRES , fill = CAUSA)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(title = "Acres Afectados por Causa en Diferentes Años",
       x = "Décadas",
       y = "Total Acres quemados") +
  theme_minimal() + theme(axis.text.x = element_text(angle = 90, hjust = 1))

  • Por AÑO (décadas)
ggplot(grpsum , aes(x = DECADA , y = PROM_ACRES)) +
  geom_bar(stat = "identity", position = "dodge", fill="#6CA6CD") +
  labs(title = "Acres Afectados en Diferentes Años",
       x = "Décadas",
       y = "Promedio Acres quemados") +
  theme_minimal() + theme(axis.text.x = element_text(angle = 90, hjust = 1)) 

  • Por CAUSA

    ggplot(grpsum , aes(x = CAUSA , y = PROM_ACRES)) +
      geom_bar(stat = "identity", position = "dodge", fill="#6CA6CD") +
      labs(title = "Acres Afectados por Diferentes Causas",
           x = "Causas",
           y = "Promedio Acres quemados") +
      theme_minimal() + theme(axis.text.x = element_text(angle = 90, hjust = 1))