【问题标题】:Function to group by and plot? - R分组和绘图的功能? -R
【发布时间】:2019-03-22 19:31:19
【问题描述】:

我是 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

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

【问题讨论】:

    标签: r ggplot2 dplyr readr


    【解决方案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")
    

    【讨论】:

      【解决方案2】:

      欢迎来到 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"))
        )
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-02
        • 2017-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多