|
2 | 2 | #' |
3 | 3 | #' Plot histogram for each continuous feature |
4 | 4 | #' @param data input data |
| 5 | +#' @param by feature name to be broken down by. If \code{NULL}, no grouping. If a continuous feature, values are grouped into 5 equal ranges; otherwise all categories of a discrete feature are used. |
5 | 6 | #' @param binary_as_factor treat binary as categorical? Default is \code{TRUE}. |
6 | 7 | #' @param geom_histogram_args a list of other arguments to \link[ggplot2]{geom_histogram} |
7 | 8 | #' @param scale_x scale of x axis. See \link[ggplot2]{scale_x_continuous} for all options. Default is \code{continuous}. |
|
21 | 22 | #' # Plot iris data |
22 | 23 | #' plot_histogram(iris, ncol = 2L) |
23 | 24 | #' |
| 25 | +#' # Plot histogram by a discrete feature |
| 26 | +#' plot_histogram(iris, by = "Species", ncol = 2L) |
| 27 | +#' |
24 | 28 | #' # Plot skewed data on log scale |
25 | 29 | #' set.seed(1) |
26 | 30 | #' skew <- data.frame(replicate(4L, rbeta(1000, 1, 5000))) |
27 | 31 | #' plot_histogram(skew, ncol = 2L) |
28 | 32 | #' plot_histogram(skew, scale_x = "log10", ncol = 2L) |
29 | 33 |
|
30 | | -plot_histogram <- function(data, binary_as_factor = TRUE, |
| 34 | +plot_histogram <- function(data, by = NULL, binary_as_factor = TRUE, |
31 | 35 | geom_histogram_args = list("bins" = 30L), |
32 | 36 | scale_x = "continuous", |
33 | 37 | title = NULL, |
34 | 38 | ggtheme = theme_gray(), theme_config = list(), |
35 | 39 | nrow = 4L, ncol = 4L, |
36 | 40 | parallel = FALSE) { |
37 | 41 | ## Declare variable first to pass R CMD check |
38 | | - variable <- value <- NULL |
| 42 | + variable <- value <- by_f <- NULL |
39 | 43 | ## Check if input is data.table |
40 | 44 | if (!is.data.table(data)) data <- data.table(data) |
41 | 45 | ## Stop if no continuous features |
42 | 46 | split_data <- split_columns(data, binary_as_factor = binary_as_factor) |
43 | 47 | if (split_data$num_continuous == 0) stop("No continuous features found!") |
44 | | - ## Get and reshape continuous features |
| 48 | + ## Get continuous features |
45 | 49 | continuous <- split_data$continuous |
46 | 50 | feature_names <- names(continuous) |
47 | | - dt <- suppressWarnings(melt.data.table(continuous, measure.vars = feature_names, variable.factor = FALSE)) |
| 51 | + if (is.null(by)) { |
| 52 | + dt <- suppressWarnings(melt.data.table(continuous, measure.vars = feature_names, variable.factor = FALSE)) |
| 53 | + dt2 <- dt |
| 54 | + } else { |
| 55 | + by_feature <- data[[by]] |
| 56 | + if (is.null(by_feature)) stop(paste0("Feature \"", by, "\" not found!")) |
| 57 | + if (is.numeric(by_feature)) { |
| 58 | + dt <- suppressWarnings(melt.data.table(data.table(continuous, "by_f" = cut_interval(by_feature, 5)), id.vars = "by_f", variable.factor = FALSE)) |
| 59 | + } else { |
| 60 | + dt <- suppressWarnings(melt.data.table(data.table(continuous, "by_f" = by_feature), id.vars = "by_f", variable.factor = FALSE)) |
| 61 | + } |
| 62 | + dt2 <- dt[variable != by] |
| 63 | + feature_names <- unique(dt2[["variable"]]) |
| 64 | + } |
48 | 65 | ## Calculate number of pages |
49 | | - layout <- .getPageLayout(nrow, ncol, ncol(continuous)) |
| 66 | + layout <- .getPageLayout(nrow, ncol, length(feature_names)) |
50 | 67 | ## Create ggplot object |
51 | 68 | plot_list <- .lapply( |
52 | 69 | parallel = parallel, |
53 | 70 | X = layout, |
54 | 71 | FUN = function(x) { |
55 | | - ggplot(dt[variable %in% feature_names[x]], aes(x = value)) + |
56 | | - do.call("geom_histogram", c("na.rm" = TRUE, geom_histogram_args)) + |
57 | | - do.call(paste0("scale_x_", scale_x), list()) + |
58 | | - ylab("Frequency") |
| 72 | + if (is.null(by)) { |
| 73 | + p <- ggplot(dt2[variable %in% feature_names[x]], aes(x = .data[["value"]])) + |
| 74 | + do.call("geom_histogram", c("na.rm" = TRUE, geom_histogram_args)) + |
| 75 | + do.call(paste0("scale_x_", scale_x), list()) + |
| 76 | + ylab("Frequency") |
| 77 | + } else { |
| 78 | + p <- ggplot(dt2[variable %in% feature_names[x]], aes(x = .data[["value"]], fill = .data[["by_f"]])) + |
| 79 | + do.call("geom_histogram", c("na.rm" = TRUE, "position" = "identity", "alpha" = 0.5, geom_histogram_args)) + |
| 80 | + do.call(paste0("scale_x_", scale_x), list()) + |
| 81 | + ylab("Frequency") + |
| 82 | + labs(fill = by) |
| 83 | + } |
| 84 | + p |
59 | 85 | } |
60 | 86 | ) |
61 | 87 | ## Plot objects |
|
0 commit comments