diff --git a/DESCRIPTION b/DESCRIPTION index 2b651a0..120d732 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -25,12 +25,19 @@ License: MIT + file LICENSE Encoding: UTF-8 LazyData: true Depends: R (>= 2.10) -Imports: stats, data.table, - officer (>= 0.3.6), cellranger, - writexl, grDevices, xml2 (>= 1.1.0), - htmltools, utils +Imports: + stats, + data.table, + officer (>= 0.3.6), + cellranger, + writexl, + grDevices, + xml2 (>= 1.1.0), + htmltools, + utils, + scales URL: https://ardata-fr.github.io/officeverse/, https://ardata-fr.github.io/mschart/ BugReports: https://github.com/ardata-fr/mschart/issues -RoxygenNote: 7.2.2 +RoxygenNote: 7.2.3 Roxygen: list(markdown = TRUE) Suggests: tinytest, doconv diff --git a/NAMESPACE b/NAMESPACE index dd8a9ce..0447c4e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -19,6 +19,7 @@ export(chart_data_size) export(chart_data_smooth) export(chart_data_stroke) export(chart_data_symbol) +export(chart_fill_ggplot2) export(chart_labels) export(chart_labels_text) export(chart_settings) @@ -30,6 +31,7 @@ export(ms_linechart) export(ms_scatterchart) export(mschart_theme) export(set_theme) +export(theme_ggplot2) importFrom(cellranger,as.cell_limits) importFrom(cellranger,as.range) importFrom(cellranger,cell_limits) diff --git a/R/themes.R b/R/themes.R new file mode 100644 index 0000000..2551a55 --- /dev/null +++ b/R/themes.R @@ -0,0 +1,79 @@ +#' Apply ggplot2 theme +#' +#' A theme that approximates the style of ggplot2::theme_grey. +#' +#' @param x a mschart object +#' @param base_size base font size +#' @param base_family font family +#' +#' @return a mschart object +#' +#' @export +#' +#' @section theme_ggplot2(): +#' +#' \if{html}{\figure{fig_theme_ggplot2.png}{options: width="500"}} +#' +#' @examples +#' p <- ms_scatterchart( +#' data = iris, x = "Sepal.Length", +#' y = "Sepal.Width", group = "Species" +#' ) +#' +#' p <- theme_ggplot2(p) +#' p <- chart_fill_ggplot2(p) +theme_ggplot2 <- function(x, base_size = 11, base_family = "Arial") { + t <- mschart_theme( + main_title = fp_text(color = "black", font.size = 1.2 * base_size, font.family = base_family), + axis_title = fp_text(color = "black", font.size = base_size, font.family = base_family), + axis_text = fp_text(color = "grey30", font.size = .8 * base_size, font.family = base_family), + axis_ticks = fp_border(color = "grey20", width = 1, style = "solid"), + grid_major_line_x = fp_border(color = "white", width = 1, style = "solid"), + grid_major_line_y = fp_border(color = "white", width = 1, style = "solid"), + grid_minor_line_x = fp_border(color = "white", width = .5, style = "solid"), + grid_minor_line_y = fp_border(color = "white", width = .5, style = "solid"), + chart_background = "white", + plot_background = "grey92", + legend_text = fp_text(color = "black", font.size = base_size, font.family = base_family), + legend_position = "r" + ) + set_theme(x, t) +} + +#' Apply ggplot2 color scale +#' +#' The default hue color scale from ggplot2. +#' +#' @param x a mschart object +#' @param stroke a boolean. Apply the color scale to stroke? Defaults to `TRUE`. +#' +#' @return a mschart object +#' +#' @export +#' +#' @section chart_fill_ggplot2(): +#' +#' \if{html}{\figure{fig_theme_ggplot2.png}{options: width="500"}} +#' +#' @examples +#' p <- ms_scatterchart( +#' data = iris, x = "Sepal.Length", +#' y = "Sepal.Width", group = "Species" +#' ) +#' +#' p <- theme_ggplot2(p) +#' p <- chart_fill_ggplot2(p) +chart_fill_ggplot2 <- function(x, stroke = TRUE) { + if (!is.null(x$group)) { + groups <- unique(x$data[[x$group]]) + ngroups <- length(groups) + pal <- scales::hue_pal()(ngroups) + names(pal) <- groups + } else { + pal <- scales::hue_pal()(1) + } + + x <- chart_data_fill(x, values = pal) + if (stroke) x <- chart_data_stroke(x, values = pal) + x +} diff --git a/man/chart_fill_ggplot2.Rd b/man/chart_fill_ggplot2.Rd new file mode 100644 index 0000000..e8c497f --- /dev/null +++ b/man/chart_fill_ggplot2.Rd @@ -0,0 +1,34 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/themes.R +\name{chart_fill_ggplot2} +\alias{chart_fill_ggplot2} +\title{Apply ggplot2 color scale} +\usage{ +chart_fill_ggplot2(x, stroke = TRUE) +} +\arguments{ +\item{x}{a mschart object} + +\item{stroke}{a boolean. Apply the color scale to stroke? Defaults to \code{TRUE}.} +} +\value{ +a mschart object +} +\description{ +The default hue color scale from ggplot2. +} +\section{chart_fill_ggplot2()}{ + + +\if{html}{\figure{fig_theme_ggplot2.png}{options: width="500"}} +} + +\examples{ +p <- ms_scatterchart( + data = iris, x = "Sepal.Length", + y = "Sepal.Width", group = "Species" +) + +p <- theme_ggplot2(p) +p <- chart_fill_ggplot2(p) +} diff --git a/man/figures/fig_theme_ggplot2.png b/man/figures/fig_theme_ggplot2.png new file mode 100644 index 0000000..0a2d070 Binary files /dev/null and b/man/figures/fig_theme_ggplot2.png differ diff --git a/man/theme_ggplot2.Rd b/man/theme_ggplot2.Rd new file mode 100644 index 0000000..d62b6cb --- /dev/null +++ b/man/theme_ggplot2.Rd @@ -0,0 +1,36 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/themes.R +\name{theme_ggplot2} +\alias{theme_ggplot2} +\title{Apply ggplot2 theme} +\usage{ +theme_ggplot2(x, base_size = 11, base_family = "Arial") +} +\arguments{ +\item{x}{a mschart object} + +\item{base_size}{base font size} + +\item{base_family}{font family} +} +\value{ +a mschart object +} +\description{ +A theme that approximates the style of ggplot2::theme_grey. +} +\section{theme_ggplot2()}{ + + +\if{html}{\figure{fig_theme_ggplot2.png}{options: width="500"}} +} + +\examples{ +p <- ms_scatterchart( + data = iris, x = "Sepal.Length", + y = "Sepal.Width", group = "Species" +) + +p <- theme_ggplot2(p) +p <- chart_fill_ggplot2(p) +}