R/plot-frequency.R

Defines functions plot_frequency

Documented in plot_frequency

#' Plot code frequency for a datavu column
#'
#' @param datavyu_object a datavyu object created by `summarize_column()`
#' @return A ggplot2 plot
#' @importFrom rlang .data


plot_frequency <- function(datavyu_object) {

  if (attributes(datavyu_object)$by_file == FALSE) {

    names(datavyu_object)[1] <- "var"

    datavyu_object %>%
      ggplot2::ggplot(ggplot2::aes(x = reorder(.data$var, .data$n),
                          y = .data$n)) +
      ggplot2::geom_col() +
      ggplot2::xlab(NULL) +
      ggplot2::coord_flip()

  } else if (attributes(datavyu_object)$by_file == TRUE) {

    # dealing with many files
    if (nrow(dplyr::count(datavyu_object, file)) >= 20) {
      message("Plotting this many files may cause problems with your plot;
              consider visualizing this data in a different way")
    }

    if (nrow(dplyr::count(datavyu_object, file)) >= 100) {
      message("Plotting this many files will almost certainly cause problems with your plot;
              consider visualizing this data in a different way")
    }

    if (nrow(dplyr::count(datavyu_object, file)) >= 200) {
      stop("Cannot plot this many files; consider visualizing this data in a different way")
    }

    names(datavyu_object)[2] <- "var"

    datavyu_object %>%
      ggplot2::ggplot(ggplot2::aes(x = reorder(.data$var, .data$n),
                          y = .data$n)) +
      ggplot2::geom_col() +
      ggplot2::coord_flip() +
      ggplot2::facet_wrap("file") +
      ggplot2::xlab(NULL)

  }
}
tca2/birdseyevyu documentation built on Dec. 23, 2021, 8:40 a.m.