在R中编写一个随时间变化的音量函数,并带有以下选项:分面或一系列情节

sgtfey8w  于 2023-06-03  发布在  其他
关注(0)|答案(1)|浏览(157)

我正在编写一个R函数,使用一些ggplot 2功能绘制一组变量随时间变化的体积,我有一个参数plot_type =“facet”或“list”。目前,分面图呈现我希望它如何,但图的列表不归因于相同(或任何)的颜色,以组变量在分面图。我知道这可能只是我的代码执行顺序的问题,导致图表列表不具有相同的颜色,但对我来说并不明显,如果有人能指出这一点,那就太好了!
下面是当前状态下的函数,我使用ifelse if参数来区分faceted和list。

plot_group_vot <-  function(data = data,
                            group_var = group_var,
                            date_var = date,
                            unit = c("day", "week", "month", "quarter", "year"),
                            nrow = 2,
                            plot_type = c("facet", "list")){

  unit <- match.arg(unit)

  date_sym <- rlang::ensym(date_var)
  group_sym <- rlang::ensym(group_var)

  if (plot_type == "facet") {
    data <- data %>% dplyr::mutate(plot_date = lubridate::floor_date(!!date_sym, unit = unit),
                                   facet_var = !!group_sym)

    plot <- data %>%
      dplyr::count(plot_date, facet_var) %>%
      ggplot2::ggplot(ggplot2::aes(x = plot_date, y = n, fill = facet_var)) +
      ggplot2::geom_col() +
      ggplot2::theme_minimal() +
      ggplot2::scale_x_date(date_breaks = "1 months", date_labels = "%d-%b") +
      ggplot2::theme(legend.position = "none",
                     axis.text.x = element_text(angle = 45),
                     panel.grid = element_blank()) +
      ggplot2::labs(title = "Topic Volume over Time", x = NULL, y = "Number of Posts") +
      ggplot2::facet_wrap(~facet_var, nrow = nrow)
    print(plot)

  } else if (plot_type == "list") {
    data <- data %>% dplyr::mutate(plot_date = lubridate::floor_date(!!date_sym, unit = unit),
                                   facet_var = !!group_sym)

    unique_groups <- data %>% pull(!!group_sym) %>% unique()
    plots <- list()

    for (group in unique_groups) {
      plot <- data %>%
        dplyr::filter(!!group_sym == group) %>%
        dplyr::count(plot_date) %>%
        ggplot2::ggplot(ggplot2::aes(x = plot_date, y = n)) +
        ggplot2::geom_col() +
        ggplot2::theme_minimal() +
        ggplot2::scale_x_date(date_breaks = "1 months", date_labels = "%d-%b") +
        ggplot2::theme(legend.position = "none",
                       axis.text.x = element_text(angle = 45),
                       panel.grid = element_blank()) +
        ggplot2::labs(title = paste("Topic Volume over Time -", group), x = NULL, y = "Number of Posts")
      plots[[group]] <- plot
    }
    return(plots)

  } else {
    stop("Invalid plot_type argument. Try 'facet' or 'list'.")
  }
}

我没有提供错误消息,但这就是我调用此函数的方式。

plot_group_vot(data = data, group_var = group_var, date_var = date, unit = "day", plot_type = "list")
w46czmvw

w46czmvw1#

多亏了一些帮助,我设法解决了这个问题,问题是我没有在我的组变量中提供一个代表组的颜色图。所以我在函数外创建了一个函数,如下所示:

colour_map <- c("group1" = "#colour1",
                "group2" = "#colour2", 
                "group3" = "#colour3")

这是函数的一部分,其中进行了更改,它的行ggplot2::geom_col(fill = colour_mapping[group])稍后将调用颜色Map。

for (group in unique_groups) {
      plot <- data %>%
        dplyr::filter(!!group_sym == group) %>%
        dplyr::count(plot_date) %>%
        ggplot2::ggplot(ggplot2::aes(x = plot_date, y = n)) +
        ggplot2::geom_col(fill = colour_mapping[group]) +
        HelpR::theme_microsoft() +
        ggplot2::scale_x_date(date_breaks = "1 months", date_labels = "%d-%b") +
        ggplot2::theme(legend.position = "none",
                       axis.text.x = element_text(angle = 45),
                       panel.grid = element_blank()) +
        ggplot2::labs(title = paste("Topic Volume over Time -", group), x = NULL, y = "Number of Posts")

      plots[[group]] <- plot
    }
    return(plots)

这意味着当想要渲染绘图列表时,现在可以像这样使用此函数:

plot_group_vot(data = data, group_var = group_var, date_var = date, unit = "day", plot_type = "list", colour_mapping = colour_map)

更新后的函数现在如下所示:

plot_group_vot <- function(data = data,
                           group_var,
                           date_var,
                           unit = c("day", "week", "month", "quarter", "year"),
                           nrow = 2,
                           plot_type = c("facet", "list"),
                           colour_mapping = NULL) {

  unit <- match.arg(unit)

  date_sym <- rlang::ensym(date_var)
  group_sym <- rlang::ensym(group_var)

  if (plot_type == "facet") {
    data <- data %>% dplyr::mutate(plot_date = lubridate::floor_date(!!date_sym, unit = unit),
                                   facet_var = !!group_sym)

    plot <- data %>%
      dplyr::count(plot_date, facet_var) %>%
      ggplot2::ggplot(ggplot2::aes(x = plot_date, y = n, fill = facet_var)) +
      ggplot2::geom_col() +
      HelpR::theme_microsoft() +
      ggplot2::scale_x_date(date_breaks = "1 months", date_labels = "%d-%b") +
      ggplot2::theme(legend.position = "none",
                     axis.text.x = element_text(angle = 45),
                     panel.grid = element_blank()) +
      ggplot2::labs(title = "Topic Volume over Time", x = NULL, y = "Number of Posts") +
      ggplot2::facet_wrap(~facet_var, nrow = nrow)

    print(plot)

  } else if (plot_type == "list") {
    data <- data %>% dplyr::mutate(plot_date = lubridate::floor_date(!!date_sym, unit = unit),
                                   facet_var = !!group_sym)

    unique_groups <- data %>% pull(!!group_sym) %>% unique()
    plots <- list()

    for (group in unique_groups) {
      plot <- data %>%
        dplyr::filter(!!group_sym == group) %>%
        dplyr::count(plot_date) %>%
        ggplot2::ggplot(ggplot2::aes(x = plot_date, y = n)) +
        ggplot2::geom_col(fill = colour_mapping[group]) +
        HelpR::theme_microsoft() +
        ggplot2::scale_x_date(date_breaks = "1 months", date_labels = "%d-%b") +
        ggplot2::theme(legend.position = "none",
                       axis.text.x = element_text(angle = 45),
                       panel.grid = element_blank()) +
        ggplot2::labs(title = paste("Topic Volume over Time -", group), x = NULL, y = "Number of Posts")

      plots[[group]] <- plot
    }
    return(plots)

  } else {
    stop("Invalid plot_type argument. Try 'facet' or 'list'.")
  }
}

相关问题