功能分组和绘图? -R

时间:2019-03-22 19:31:19

标签: r ggplot2 dplyr readr

我是R的新手,并且正在从DataQuest学习R。我有csv的森林大火。该文件可以在这里下载:

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

我想创建一个将数据按“ x”(例如月或日)分组并返回计数的条形图的函数。

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

当我调用该函数时,如何指出月份和日期是列?

2 个答案:

答案 0 :(得分:1)

欢迎使用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"))
  )

答案 1 :(得分:1)

您可以尝试以下操作:

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