分组和绘图的功能? -R

Function to group by and plot? - R

我是 R 的新手,正在学习 DataQuest 的 R 课程。我有一个森林火灾的 csv。文件可以在这里下载:

https://archive.ics.uci.edu/ml/machine-learning-databases/forest-fires/

我想创建一个函数,按 "x"(例如月或日)和 return 计数条形图对数据进行分组。

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

当我调用函数时,如何声明月份和日期是列?

欢迎来到 dplyr/ggplot2/tidyverse 的编程世界。您会想要 read more about the details here,但以下内容将助您一臂之力:

library(tidyverse)

df <- read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/forest-fires/forestfires.csv")

plot_group <- function(df, grp) {
  grp_var <- enquo(grp)
  df %>%
    count(!! grp_var) %>%
    ggplot(aes(x = !!grp_var, y = n)) +
    geom_col()
}

plot_group(df, month)
plot_group(df, day)

注意:您可能需要先重新调整 monthday 变量,以便它们以更符合预期的顺序绘制:

df <- df %>%
  mutate(
    month = fct_relevel(month, str_to_lower(month.abb)),
    day = fct_relevel(day, c("sun", "mon", "tue", "wed", "thu", "fri", "sat"))
  )

您可以尝试这样的操作:

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")