R/myplot.R

Defines functions myplot

utils::globalVariables("value")
myplot <- function(x, y){
  classes <- sapply(x, class)
  if (is.data.frame(x)){
  } else {
    stop("Your first argument must be a data frame!")
  }
  if (eval(nrow(x) <  1)){
    stop("Your data frame must have at least one row!")
  }
  if (y == "integer"){
    if (y %in% classes){
      x %>%
        purrr::keep(is.integer) %>%
        tidyr::gather() %>%
        ggplot2::ggplot(ggplot2::aes(value)) +
        ggplot2::facet_wrap(~ key, scales = "free") +
        ggplot2::ggtitle(paste0("Variables in ",deparse(substitute(x)), " of class ", deparse(y))) +
        ggplot2::geom_histogram(bins=30)
    } else {
      stop(paste0(deparse(substitute(x)), " has no variables of class ", deparse(y)))
    }
  }
  else if (y == "double"){
    if (y %in% classes){
      x %>%
        purrr::keep(is.double) %>%
        tidyr::gather() %>%
        ggplot2::ggplot(ggplot2::aes(value)) +
        ggplot2::facet_wrap(~ key, scales = "free") +
        ggplot2::ggtitle(paste0("Variables in ",deparse(substitute(x)), " of class ", deparse(y))) +
        ggplot2::geom_histogram(bins=30)
    } else {
      stop(paste0(deparse(substitute(x)), " has no variables of class ", deparse(y)))
    }
  }
  else if (y == "numeric"){
    if (y %in% classes){
      x %>%
        purrr::keep(is.numeric) %>%
        tidyr::gather() %>%
        ggplot2::ggplot(ggplot2::aes(value)) +
        ggplot2::facet_wrap(~ key, scales = "free") +
        ggplot2::ggtitle(paste0("Variables in ",deparse(substitute(x)), " of class ", deparse(y))) +
        ggplot2::geom_histogram(bins=30)
    } else {
      stop(paste0(deparse(substitute(x)), " has no variables of class ", deparse(y)))
    }
  }
  else if (y == "character"){
    if (y %in% classes){
      x %>%
        purrr::keep(is.character) %>%
        tidyr::gather() %>%
        ggplot2::ggplot(ggplot2::aes(value)) +
        ggplot2::facet_wrap(~ key, scales = "free") +
        ggplot2::ggtitle(paste0("Variables in ",deparse(substitute(x)), " of class ", deparse(y))) +
        ggplot2::geom_bar(bins=30)
    } else {
      stop(paste0(deparse(substitute(x)), " has no variables of class ", deparse(y)))
    }
  }
  else if (y == "factor"){
    if (y %in% classes){
      x %>%
        purrr::keep(is.factor) %>%
        tidyr::gather() %>%
        ggplot2::ggplot(ggplot2::aes(value)) +
        ggplot2::facet_wrap(~ key, scales = "free") +
        ggplot2::ggtitle(paste0("Variables in ",deparse(substitute(x)), " of class ", deparse(y))) +
        ggplot2::geom_bar(bins=30)
    } else {
      stop(paste0(deparse(substitute(x)), " has no variables of class ", deparse(y)))
    }
  }
  else if (y == "logical"){
    if (y %in% classes){
      x %>%
        purrr::keep(is.logical) %>%
        tidyr::gather() %>%
        ggplot2::ggplot(ggplot2::aes(value)) +
        ggplot2::facet_wrap(~ key, scales = "free") +
        ggplot2::ggtitle(paste0("Variables in ",deparse(substitute(x)), " of class ", deparse(y))) +
        ggplot2::geom_bar(bins=30)
    } else {
      stop(paste0(deparse(substitute(x)), " has no variables of class ", deparse(y)))
    }
  }
}
22S-DATA-413-613/hw06pdbernal1 documentation built on April 4, 2022, 12:37 a.m.