I'm new to R and I'm doing the R course from DataQuest. I have a csv of forest fires. The file can be downloaded here:
https://archive.ics.uci.edu/ml/machine-learning-databases/forest-fires/
I want to create a function that groups the data by "x" (eg. month or day) and return a bar chart of the count.
library(readr)
library(dplyr)
library(ggplot2)
forestFires <- read_csv("forestfires.csv")
forestFiresCountPlot <- function(x) {
forestFiresGroup <- forestFires %>%
group_by(x) %>%
summarise(n(x)) %>%
ggplot(data = forestFiresGroup) +
aes(x = x, y = n(x)) +
geom_bar()
}
forestFiresMonth <- forestFiresCountPlot(month)
forestFiresDay <- forestFiresCountPlot(day)
# Output - Error: Column `x` is unknown
When I call the function, how do I state that month and day are columns?
You can try something like this:
forestFiresCountPlot <- function(x) {
forestFires %>%
group_by_at(x) %>%
summarize(n = n()) %>%
ggplot() +
aes_string(x = x, y = “n”) +
geom_bar(stat = "identity")
}
forestFiresCountPlot("month")
forestFiresCountPlot("day")