Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
* Add theme_ggplot2() and chart_fill_ggplot2().
* Add theme_ggplot2() example illustration.
  • Loading branch information
trekonom committed May 11, 2023
1 parent 3675665 commit 2ac7be0
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 5 deletions.
17 changes: 12 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
79 changes: 79 additions & 0 deletions R/themes.R
Original file line number Diff line number Diff line change
@@ -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
}
34 changes: 34 additions & 0 deletions man/chart_fill_ggplot2.Rd

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

Binary file added man/figures/fig_theme_ggplot2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions man/theme_ggplot2.Rd

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

0 comments on commit 2ac7be0

Please # to comment.