diff --git a/NAMESPACE b/NAMESPACE index cb6928d..2625789 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -26,6 +26,7 @@ export(apply_row) export(array_2dTo3d) export(array_3dTo2d) export(as.data.table) +export(bold) export(box_qtl) export(cdo_grid) export(char2expr) @@ -58,7 +59,6 @@ export(edit_r_environ) export(edit_r_profile) export(edit_r_profile_sys) export(evince) -export(facet_tag) export(file_ext) export(file_name) export(file_size) @@ -72,13 +72,13 @@ export(gc_cluster) export(gc_linux) export(geom_boxplot2) export(getwd_clip) -export(ggplot_legend) export(git_commit) export(git_commit_amend) export(git_push) export(git_set_remote) export(github) export(glue) +export(green) export(group_apply) export(guess_names) export(icount) @@ -128,6 +128,7 @@ export(progressively) export(quantile_envelope) export(read_xlsx) export(read_xlsx2list) +export(red) export(reorder_name) export(revalue) export(rm_empty) @@ -152,6 +153,7 @@ export(top_frac) export(top_n) export(touch) export(transpose) +export(underline) export(unique_length) export(upper_envelope) export(use_build_ignore) @@ -193,10 +195,7 @@ importFrom(dplyr,top_n) importFrom(foreach,"%do%") importFrom(foreach,"%dopar%") importFrom(foreach,foreach) -importFrom(ggplot2,aes_string) -importFrom(ggplot2,geom_text) -importFrom(ggplot2,ggplot_build) -importFrom(ggplot2,ggplot_gtable) +importFrom(ggplot2,Stat) importFrom(grDevices,cairo_pdf) importFrom(grDevices,col2rgb) importFrom(grDevices,colorRampPalette) diff --git a/R/color_terminal.R b/R/color_terminal.R index cfa86a3..f46834b 100644 --- a/R/color_terminal.R +++ b/R/color_terminal.R @@ -1,3 +1,15 @@ +#' @export +crayon::bold + +#' @export +crayon::red + +#' @export +crayon::green + +#' @export +crayon::underline + #' Colored terminal output #' #' @param ... Strings to style. diff --git a/R/geom_boxplot2.R b/R/geom_boxplot2.R index e9dce86..1d3d527 100644 --- a/R/geom_boxplot2.R +++ b/R/geom_boxplot2.R @@ -1,3 +1,18 @@ +#' @name ggplot2-ggproto +#' @title ggplot2-ggproto +#' +#' @format NULL +#' @usage NULL +#' @keywords internal +#' @importFrom ggplot2 Stat +NULL + +check_required_aesthetics <- ggplot2:::check_required_aesthetics +ggproto_formals <- ggplot2:::ggproto_formals +snake_class <- ggplot2:::snake_class +uniquecols <- ggplot2:::uniquecols +has_groups <- ggplot2:::has_groups + #' A box and whiskers plot (in the style of Tukey) #' #' The boxplot compactly displays the distribution of a continuous variable. diff --git a/R/ggplot-facet_tag.R b/R/ggplot-facet_tag.R deleted file mode 100644 index 0c147a8..0000000 --- a/R/ggplot-facet_tag.R +++ /dev/null @@ -1,50 +0,0 @@ -#' @importFrom ggplot2 ggplot_gtable -#' @export -ggplot_legend<-function(g){ - tmp <- ggplot_gtable(ggplot_build(g)) - leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box") - legend <- tmp$grobs[[leg]] - return(legend) -} - -#' @importFrom ggplot2 ggplot_build geom_text aes_string -#' @export -facet_tag <- function (p, open = "(", close = ")", tag_pool = letters, x = -Inf, - I_start = 1, - update_theme = FALSE, - y = Inf, hjust = -0.5, vjust = 1.5, fontface = 2, fontsize = 14, family = "TimesSimSun", - ...) -{ - gb <- ggplot_build(p) - lay <- gb$layout$layout - tags <- cbind(lay, label = paste0(open, tag_pool[as.numeric(lay$PANEL) + I_start - 1], - close), x = x, y = y) - p <- p + geom_text(data = tags, aes_string(x = "x", y = "y", label = "label"), - ..., hjust = hjust, vjust = vjust, fontface = fontface, size = fontsize, - family = family, inherit.aes = FALSE) - - # update_theme = TRUE - if (update_theme) { - p <- p + - theme( - panel.background = element_rect(fill = "white"), - panel.border = element_rect(fill = NA, color = "grey60", size = 0.5), - strip.text = element_text(size = fontsize - 1, family = family), - # strip.text = element_blank(), - strip.background = element_blank(), - axis.title = element_text(size = fontsize, face = 2), - legend.title = element_text(size = fontsize+1, face = 2), - legend.text = element_text(size = fontsize+1) - ) - } - p -} - -# theme( -# legend.position = "bottom", -# axis.text = element_text(color = "black", size = 17), -# plot.margin = margin(-5, 0, 0, 0), -# legend.box.margin = margin(-5, 0, 0, 0), -# legend.text = element_text(size = 18), -# axis.title.x = element_text(margin = margin(0.2, 0, -0.3, 0, "cm")) -# ) diff --git a/R/ggplot-stat-.r b/R/ggplot-stat-.r deleted file mode 100644 index 30c2e1a..0000000 --- a/R/ggplot-stat-.r +++ /dev/null @@ -1,156 +0,0 @@ -#' @name ggplot2-ggproto -#' @title ggplot2-ggproto -#' -#' @section Stats: -#' -#' All `stat_*` functions (like `stat_bin`) return a layer that -#' contains a `Stat*` object (like `StatBin`). The `Stat*` -#' object is responsible for rendering the data in the plot. -#' -#' Each of the `Stat*` objects is a [ggproto()] object, descended -#' from the top-level `Stat`, and each implements various methods and -#' fields. To create a new type of Stat object, you typically will want to -#' override one or more of the following: -#' -#' - One of : -#' `compute_layer(self, data, scales, ...)`, -#' `compute_panel(self, data, scales, ...)`, or -#' `compute_group(self, data, scales, ...)`. -#' -#' `compute_layer()` is called once per layer, `compute_panel_()` -#' is called once per panel, and `compute_group()` is called once per -#' group. All must return a data frame. -#' -#' It's usually best to start by overriding `compute_group`: if -#' you find substantial performance optimisations, override higher up. -#' You'll need to read the source code of the default methods to see -#' what else you should be doing. -#' -#' `data` is a data frame containing the variables named according -#' to the aesthetics that they're mapped to. `scales` is a list -#' containing the `x` and `y` scales. There functions are called -#' before the facets are trained, so they are global scales, not local -#' to the individual panels.`...` contains the parameters returned by -#' `setup_params()`. -#' - `finish_layer(data, params)`: called once for each layer. Used -#' to modify the data after scales has been applied, but before the data is -#' handed of to the geom for rendering. The default is to not modify the -#' data. Use this hook if the stat needs access to the actual aesthetic -#' values rather than the values that are mapped to the aesthetic. -#' - `setup_params(data, params)`: called once for each layer. -#' Used to setup defaults that need to complete dataset, and to inform -#' the user of important choices. Should return list of parameters. -#' - `setup_data(data, params)`: called once for each layer, -#' after `setup_params()`. Should return modified `data`. -#' Default methods removes all rows containing a missing value in -#' required aesthetics (with a warning if `!na.rm`). -#' - `required_aes`: A character vector of aesthetics needed to -#' render the geom. -#' - `default_aes`: A list (generated by [aes()] of -#' default values for aesthetics. -#' @format NULL -#' @usage NULL -#' @keywords internal -Stat <- ggproto("Stat", - # Should the values produced by the statistic also be transformed - # in the second pass when recently added statistics are trained to - # the scales - retransform = TRUE, - - default_aes = aes(), - - required_aes = character(), - - non_missing_aes = character(), - - setup_params = function(data, params) { - params - }, - - setup_data = function(data, params) { - data - }, - - compute_layer = function(self, data, params, layout) { - check_required_aesthetics( - self$required_aes, - c(names(data), names(params)), - snake_class(self) - ) - - data <- remove_missing(data, params$na.rm, - c(self$required_aes, self$non_missing_aes), - snake_class(self), - finite = TRUE - ) - - # Trim off extra parameters - params <- params[intersect(names(params), self$parameters())] - - args <- c(list(data = quote(data), scales = quote(scales)), params) - plyr::ddply(data, "PANEL", function(data) { - scales <- layout$get_scales(data$PANEL[1]) - tryCatch(do.call(self$compute_panel, args), error = function(e) { - warning("Computation failed in `", snake_class(self), "()`:\n", - e$message, call. = FALSE) - data.frame() - }) - }) - }, - - compute_panel = function(self, data, scales, ...) { - if (empty(data)) return(data.frame()) - - groups <- split(data, data$group) - stats <- lapply(groups, function(group) { - self$compute_group(data = group, scales = scales, ...) - }) - - stats <- mapply(function(new, old) { - if (empty(new)) return(data.frame()) - unique <- uniquecols(old) - missing <- !(names(unique) %in% names(new)) - cbind( - new, - unique[rep(1, nrow(new)), missing,drop = FALSE] - ) - }, stats, groups, SIMPLIFY = FALSE) - - do.call(plyr::rbind.fill, stats) - }, - - compute_group = function(self, data, scales) { - stop("Not implemented", call. = FALSE) - }, - - finish_layer = function(self, data, params) { - data - }, - - # See discussion at Geom$parameters() - extra_params = "na.rm", - parameters = function(self, extra = FALSE) { - # Look first in compute_panel. If it contains ... then look in compute_group - panel_args <- names(ggproto_formals(self$compute_panel)) - group_args <- names(ggproto_formals(self$compute_group)) - args <- if ("..." %in% panel_args) group_args else panel_args - - # Remove arguments of defaults - args <- setdiff(args, names(ggproto_formals(Stat$compute_group))) - - if (extra) { - args <- union(args, self$extra_params) - } - args - }, - - aesthetics = function(self) { - c(union(self$required_aes, names(self$default_aes)), "group") - } -) - -check_required_aesthetics <- ggplot2:::check_required_aesthetics -ggproto_formals <- ggplot2:::ggproto_formals -snake_class <- ggplot2:::snake_class -uniquecols <- ggplot2:::uniquecols -has_groups <- ggplot2:::has_groups diff --git a/README.md b/README.md index 6e79e4b..02e04ea 100644 --- a/README.md +++ b/README.md @@ -43,15 +43,15 @@ After install, run `Ipaper::key_blind()` to make those shortcuts work. ### Visualization -- `draw.colorkey`: modified from lattice, add triangle head and tail + -- `ggplot_legend`: get the legend (grid obj) of ggplot object + - `write_fig`: Unify figure writing functions, e.g. png, pdf, tif, svg diff --git a/man/ggplot2-ggproto.Rd b/man/ggplot2-ggproto.Rd index 4beb1d0..26834e7 100644 --- a/man/ggplot2-ggproto.Rd +++ b/man/ggplot2-ggproto.Rd @@ -1,62 +1,9 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/ggplot-stat-.r -\docType{data} +% Please edit documentation in R/geom_boxplot2.R \name{ggplot2-ggproto} \alias{ggplot2-ggproto} -\alias{Stat} \title{ggplot2-ggproto} \description{ ggplot2-ggproto } -\section{Stats}{ - - -All \verb{stat_*} functions (like \code{stat_bin}) return a layer that -contains a \verb{Stat*} object (like \code{StatBin}). The \verb{Stat*} -object is responsible for rendering the data in the plot. - -Each of the \verb{Stat*} objects is a \code{\link[=ggproto]{ggproto()}} object, descended -from the top-level \code{Stat}, and each implements various methods and -fields. To create a new type of Stat object, you typically will want to -override one or more of the following: -\itemize{ -\item One of : -\code{compute_layer(self, data, scales, ...)}, -\code{compute_panel(self, data, scales, ...)}, or -\code{compute_group(self, data, scales, ...)}. - -\code{compute_layer()} is called once per layer, \code{compute_panel_()} -is called once per panel, and \code{compute_group()} is called once per -group. All must return a data frame. - -It's usually best to start by overriding \code{compute_group}: if -you find substantial performance optimisations, override higher up. -You'll need to read the source code of the default methods to see -what else you should be doing. - -\code{data} is a data frame containing the variables named according -to the aesthetics that they're mapped to. \code{scales} is a list -containing the \code{x} and \code{y} scales. There functions are called -before the facets are trained, so they are global scales, not local -to the individual panels.\code{...} contains the parameters returned by -\code{setup_params()}. -\item \code{finish_layer(data, params)}: called once for each layer. Used -to modify the data after scales has been applied, but before the data is -handed of to the geom for rendering. The default is to not modify the -data. Use this hook if the stat needs access to the actual aesthetic -values rather than the values that are mapped to the aesthetic. -\item \code{setup_params(data, params)}: called once for each layer. -Used to setup defaults that need to complete dataset, and to inform -the user of important choices. Should return list of parameters. -\item \code{setup_data(data, params)}: called once for each layer, -after \code{setup_params()}. Should return modified \code{data}. -Default methods removes all rows containing a missing value in -required aesthetics (with a warning if \code{!na.rm}). -\item \code{required_aes}: A character vector of aesthetics needed to -render the geom. -\item \code{default_aes}: A list (generated by \code{\link[=aes]{aes()}} of -default values for aesthetics. -} -} - \keyword{internal} diff --git a/man/reexports.Rd b/man/reexports.Rd index 50fb9a3..055dc99 100644 --- a/man/reexports.Rd +++ b/man/reexports.Rd @@ -1,8 +1,13 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/main_dt_ddply.R, R/reexports.R +% Please edit documentation in R/color_terminal.R, R/main_dt_ddply.R, +% R/reexports.R \docType{import} \name{reexports} \alias{reexports} +\alias{bold} +\alias{red} +\alias{green} +\alias{underline} \alias{.} \alias{mapvalues} \alias{revalue} @@ -43,6 +48,8 @@ These objects are imported from other packages. Follow the links below to see their documentation. \describe{ + \item{crayon}{\code{\link[crayon:crayon]{bold}}, \code{\link[crayon:crayon]{green}}, \code{\link[crayon:crayon]{red}}, \code{\link[crayon:crayon]{underline}}} + \item{data.table}{\code{\link[data.table]{as.data.table}}, \code{\link[data.table]{data.table}}, \code{\link[data.table:as.data.table]{is.data.table}}} \item{dplyr}{\code{\link[dplyr:nth]{first}}, \code{\link[dplyr:nth]{last}}, \code{\link[dplyr]{mutate}}, \code{\link[dplyr:top_n]{top_frac}}, \code{\link[dplyr]{top_n}}}