Queer European MD passionate about IT

Mission277Solutions.Rmd 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. ---
  2. title: "Solutions for Guided Project: Exploratory Visualization of Forest Fire Data"
  3. author: "Rose Martin"
  4. dat:e "December 4, 2018"
  5. output: html_document
  6. ---
  7. Load the packages we will need for the exercise:
  8. ```{r}
  9. library(readr)
  10. library(dplyr)
  11. library(ggplot2)
  12. library(purrr)
  13. ```
  14. Import the data file. Save it as a data frame.
  15. ```{r}
  16. forest_fires <- read_csv("forestfires.csv")
  17. ```
  18. Create a bar chart showing the number of forest fires occuring during each month
  19. ```{r}
  20. fires_by_month <- forest_fires %>%
  21. group_by(month) %>%
  22. summarize(total_fires = n())
  23. ggplot(data = fires_by_month) +
  24. aes(x = month, y = total_fires) +
  25. geom_bar(stat = "identity") +
  26. theme(panel.background = element_rect(fill = "white"),
  27. axis.line = element_line(size = 0.25,
  28. colour = "black"))
  29. ```
  30. Create a bar chart showing the number of forest fires occurring on each day of the week
  31. ```{r}
  32. fires_by_DOW <- forest_fires %>%
  33. group_by(day) %>%
  34. summarize(total_fires = n())
  35. ggplot(data = fires_by_DOW) +
  36. aes(x = day, y = total_fires) +
  37. geom_bar(stat = "identity") +
  38. theme(panel.background = element_rect(fill = "white"),
  39. axis.line = element_line(size = 0.25,
  40. colour = "black"))
  41. ```
  42. Change the data type of month to factor and specify the order of months
  43. ```{r}
  44. forest_fires <- forest_fires %>%
  45. mutate(month = factor(month, levels = c("jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec")),
  46. day = factor(day, levels = c("sun", "mon", "tue", "wed", "thu", "fri", "sat")))
  47. ## once you have reordered the months and days of the week, you can re-run the bar chart code above
  48. # to create new bar graphs
  49. ```
  50. Write a function to create a boxplot for visualizing variable distributions by month and day of the week
  51. ```{r}
  52. ## Write the function
  53. create_boxplots <- function(x, y) {
  54. ggplot(data = forest_fires) +
  55. aes_string(x = x, y = y) +
  56. geom_boxplot() +
  57. theme(panel.background = element_rect(fill = "white"))
  58. }
  59. ## Assign x and y variable names
  60. x_var_month <- names(forest_fires)[3] ## month
  61. x_var_day <- names(forest_fires)[4] ## day
  62. y_var <- names(forest_fires)[5:12]
  63. ## use the map() function to apply the function to the variables of interest
  64. month_box <- map2(x_var_month, y_var, create_boxplots) ## visualize variables by month
  65. day_box <- map2(x_var_day, y_var, create_boxplots) ## visualize variables by day
  66. ```
  67. Create scatter plots to see which variables may affect forest fire size:
  68. ```{r}
  69. ## write the function
  70. create_scatterplots = function(x, y) {
  71. ggplot(data = forest_fires) +
  72. aes_string(x = x, y = y) +
  73. geom_point() +
  74. theme(panel.background = element_rect(fill = "white"))
  75. }
  76. ## Assign x and y variable names
  77. x_var_scatter <- names(forest_fires)[5:12]
  78. y_var_scatter <- names(forest_fires)[13]
  79. ## use the map() function to apply the function to the variables of interest
  80. scatters <- map2(x_var_scatter, y_var_scatter, create_scatterplots)
  81. ```