Skip to content

Commit

Permalink
Fixed #38
Browse files Browse the repository at this point in the history
  • Loading branch information
boxuancui committed Dec 21, 2016
1 parent 7af42ad commit cfb96ab
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ importFrom(scales,comma)
importFrom(scales,percent)
importFrom(stats,cor)
importFrom(stats,na.omit)
importFrom(stats,reorder)
importFrom(utils,browseURL)
importFrom(utils,capture.output)
importFrom(utils,str)
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#### Enhancements
* [#36](https://github.com/boxuancui/DataExplorer/issues/36): Fixed warnings from data.table in `DropVar`.
* [#37](https://github.com/boxuancui/DataExplorer/issues/37): Changed all `cat()` to `message()`.
* [#38](https://github.com/boxuancui/DataExplorer/issues/38): Added option to order bars in `BarDiscrete`.
* Added more examples in README file.

---
Expand Down
20 changes: 14 additions & 6 deletions R/BarDiscrete.r
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
#' @param data input data to be plotted, in either \link{data.frame} or \link{data.table} format.
#' @param na.rm logical, indicating if missing values should be removed for each feature. The default is \code{TRUE}.
#' @param maxcat maximum categories allowed for each feature. The default is 50. More information in 'Details' section.
#' @param order_bar logical, indicating if bars should be ordered.
#' @keywords bardiscrete
#' @details If a discrete feature contains more categories than \code{maxcat} specifies, it will not be passed to the plotting function.
#' @import data.table
#' @import ggplot2
#' @importFrom scales comma
#' @importFrom stats na.omit
#' @importFrom stats na.omit reorder
#' @import gridExtra
#' @export
#' @examples
Expand All @@ -24,7 +25,10 @@
#' BarDiscrete(diamonds)
#' BarDiscrete(diamonds, maxcat = 5)

BarDiscrete <- function(data, na.rm = TRUE, maxcat = 50) {
BarDiscrete <- function(data, na.rm = TRUE, maxcat = 50, order_bar = TRUE) {
## Declare variable first to pass R CMD check
frequency <- NULL
## Check if input is data.table
if (!is.data.table(data)) {data <- data.table(data)}
## Stop if no discrete features
if (SplitColType(data)$num_discrete == 0) stop("No Discrete Features")
Expand All @@ -51,16 +55,20 @@ BarDiscrete <- function(data, na.rm = TRUE, maxcat = 50) {
x <- subset_data[, j, with = FALSE]
agg_x <- x[, list(frequency = .N), by = names(x)]
if (na.rm) {agg_x <- na.omit(agg_x)}
ggplot(agg_x, aes_string(x = names(agg_x)[1], y = "frequency")) +
if (order_bar) {
base_plot <- ggplot(agg_x, aes(x = reorder(get(names(agg_x)[1]), frequency), y = frequency))
} else {
base_plot <- ggplot(agg_x, aes_string(x = names(agg_x)[1], y = "frequency"))
}
base_plot +
geom_bar(stat = "identity", alpha = 0.4, colour = "black") +
scale_y_continuous(labels = comma) +
coord_flip() + ylab("Frequency")
coord_flip() + xlab(names(agg_x)[1]) + ylab("Frequency")
})
## Print plot object
if (pages > 1) {
suppressWarnings(do.call(grid.arrange, c(plot, ncol = 3, nrow = 3)))
}
else {
} else {
suppressWarnings(do.call(grid.arrange, plot))
}
}
Expand Down
2 changes: 1 addition & 1 deletion R/GenerateReport.r
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ GenerateReport <- function(input_data, output_file = "report.html", output_dir =
output_file = output_file,
output_dir = output_dir,
intermediates_dir = output_dir,
params=list(data = input_data, fun_options = list()),
params = list(data = input_data, fun_options = list()),
...
)
## Open report
Expand Down
4 changes: 3 additions & 1 deletion man/BarDiscrete.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cfb96ab

Please sign in to comment.