Forest Fire Project in R Data Visualisation

Hi, I am attempting the guided project for data visualisation in R. I have been able to get the first few steps. However, I was not able mutate my month and day of week data from a char to factor. Below is the code I have currently written.

My Code:

#Set working directory
setwd('/Users/adayummmm/RStudio Things/Dataquest/Analysing Forest Data')

# Loading relevant libraries
library(readr)
library(ggplot2)
library(dplyr)
library(purrr)

#importing data into R Studio
forestfires <- read_csv('forestfires.csv')

#Bar Chart of forest fire by Month
forestfires_Month <- forestfires %>% group_by(month) %>% summarise(total_fires = n())

Bar_Months <- ggplot(data = forestfires_Month) + 
        aes(x = month, y = total_fires) + 
        geom_bar(stat = 'identity') +
        labs(title = 'Total Forest Fires by Month', 
             y = 'Number of Fires', 
             x = 'Month' ) +
        theme(panel.background = element_rect(fill = 'white'), 
              axis.line = element_line(size = 0.25, colour = 'black'),
              plot.title = element_text(hjust = 0.5))
#Bar Chart of forest fire by Days
forestfires_DayofWeek <- forestfires %>% group_by(day) %>% summarise(total_fires = n())

Bar_DOW <- ggplot(data = forestfires_DayofWeek) +
        aes(x = day, y = total_fires) +
        geom_bar(stat = 'identity') +
        labs(title = 'Total Forest Fires by Day of Week',
             x = 'Day of the Week',
             y = 'Number of Fires') +
        theme(panel.background = element_rect(fill = 'white'),
              axis.line = element_line(size = 0.25, colour = 'black'),
              plot.title = element_text(hjust = 0.5))

#Changing data type of months to factor
forestfires <- forestfires %>% mutate(
                month = factor(month, levels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")), 
               day = factor(day, levels = c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"))
               )





I was hoping that my Bar_Month and Bar_DOW would have the x-axis rearranged to the order I specified in my mutate()

However, there were no changes to the plots.

Hi @adamjosh26. The issue is that the levels provided here for month and day are capitalized, but in the dataset the values are all lower case. What’s going on here is that all of the lower case values in month and day are silently converted to NA because there is no match for them provided in the factor levels that are defined.

This code worked for me:

# Loading relevant libraries
library(readr)
library(ggplot2)
library(dplyr)
library(purrr)

#importing data into R Studio
forestfires <- read_csv('forestfires.csv')

#Changing data type of months to factor
forestfires <- forestfires %>% mutate(
  month = factor(month, levels = c("jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec")), 
  day = factor(day, levels = c("mon", "tue", "wed", "thu", "fri", "sat", "sun"))
)

#Bar Chart of forest fire by Month
forestfires_Month <- forestfires %>% group_by(month) %>% summarise(total_fires = n())

Bar_Months <- ggplot(data = forestfires_Month) + 
  aes(x = month, y = total_fires) + 
  geom_bar(stat = 'identity') +
  labs(title = 'Total Forest Fires by Month', 
       y = 'Number of Fires', 
       x = 'Month' ) +
  theme(panel.background = element_rect(fill = 'white'), 
        axis.line = element_line(size = 0.25, colour = 'black'),
        plot.title = element_text(hjust = 0.5))
#Bar Chart of forest fire by Days
forestfires_DayofWeek <- forestfires %>% group_by(day) %>% summarise(total_fires = n())

Bar_DOW <- ggplot(data = forestfires_DayofWeek) +
  aes(x = day, y = total_fires) +
  geom_bar(stat = 'identity') +
  labs(title = 'Total Forest Fires by Day of Week',
       x = 'Day of the Week',
       y = 'Number of Fires') +
  theme(panel.background = element_rect(fill = 'white'),
        axis.line = element_line(size = 0.25, colour = 'black'),
        plot.title = element_text(hjust = 0.5))

This type of thing is very common with coding. To de-bug this situation I recommend going step-by-step through the code to determine where things no longer look as expected. In this situation, the NA values were apparent after I inspected the dataframe to inspect whether or not the factor conversions worked.

Hope this helps! Best,
-Casey