diff --git a/NAMESPACE b/NAMESPACE index b3cad8e..be63dbe 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,7 @@ export("%>%") export(renderUPlot) +export(uColors) export(uPlot) export(uPlotOutput) export(uSeries) diff --git a/R/uSeries.R b/R/uSeries.R index 566b58d..50fc492 100644 --- a/R/uSeries.R +++ b/R/uSeries.R @@ -38,3 +38,33 @@ find_serie_index <- function(uplot, name) { return(index) } + + + +#' Series colors +#' +#' Allow to specify series colors in one call. +#' +#' @param uplot Chart created with [uPlot()]. +#' @param ... Colors to attribute to each series. +#' +#' @return An `htmlwidget` object of class `"uPlot"`. +#' @export +#' +#' @examples +#' uPlot(temperatures[, c("date", "temperature")]) %>% +#' uColors(temperature = "black") +#' +#' uPlot(temperatures[, c("date", "low", "high")]) %>% +#' uColors( +#' low = "blue", +#' high = "red" +#' ) +uColors <- function(uplot, ...) { + args <- list(...) + for (i in seq_along(args)) { + uplot <- uSeries(uplot, name = names(args)[i], stroke = args[[i]]) + } + uplot +} + diff --git a/R/utils.R b/R/utils.R index 05977f4..5301a3d 100644 --- a/R/utils.R +++ b/R/utils.R @@ -17,7 +17,7 @@ prepare_data <- function(.data) { stopifnot(".data must be a data.frame or equivalent" = is.data.frame(.data)) stopifnot(".data must have at least 2 columns" = ncol(.data) >= 2) stopifnot("First column of .data must be either a numeric or a POSIXct" = inherits(.data[[1]], c("numeric", "POSIXct", "Date"))) - stopifnot("All columns except first one of .data must numeric" = all_numeric(.data[, -1])) + stopifnot("All columns except first one of .data must be numeric" = all_numeric(.data[, -1])) .nms <- names(.data) .data <- unname(as.list(.data)) if (inherits(.data[[1]], c("Date"))) { diff --git a/README.md b/README.md index 6995c80..4bf787d 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,6 @@ -:warning: Doesn't work in RStudio viewer. :warning: - - ## Installation You can install the development version of uPlot from [GitHub](https://github.com/dreamRs/uPlot-r) with: @@ -33,15 +30,17 @@ uPlot( title = "Electricity production by sources in France (2012 - 2022)" ) ) %>% - uSeries(name = "fuel", stroke = "#80549f") %>% - uSeries(name = "coal", stroke = "#a68832") %>% - uSeries(name = "gas", stroke = "#f20809") %>% - uSeries(name = "nuclear", stroke = "#e4a701") %>% - uSeries(name = "wind", stroke = "#72cbb7") %>% - uSeries(name = "solar", stroke = "#d66b0d") %>% - uSeries(name = "hydraulic", stroke = "#2672b0") %>% - uSeries(name = "pumping", stroke = "#0e4269") %>% - uSeries(name = "bioenergies", stroke = "#156956") + uColors( + "bioenergies" = "#156956", + "fuel" = "#80549f", + "coal" = "#a68832", + "solar" = "#d66b0d", + "gas" = "#f20809", + "wind" = "#72cbb7", + "hydraulic" = "#2672b0", + "nuclear" = "#e4a701", + "pumping" = "#0e4269" + ) ``` ![uPlot example](man/figures/uplot.png) diff --git a/man/uColors.Rd b/man/uColors.Rd new file mode 100644 index 0000000..0f79122 --- /dev/null +++ b/man/uColors.Rd @@ -0,0 +1,29 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/uSeries.R +\name{uColors} +\alias{uColors} +\title{Series colors} +\usage{ +uColors(uplot, ...) +} +\arguments{ +\item{uplot}{Chart created with \code{\link[=uPlot]{uPlot()}}.} + +\item{...}{Colors to attribute to each series.} +} +\value{ +An \code{htmlwidget} object of class \code{"uPlot"}. +} +\description{ +Allow to specify series colors in one call. +} +\examples{ +uPlot(temperatures[, c("date", "temperature")]) \%>\% + uColors(temperature = "black") + +uPlot(temperatures[, c("date", "low", "high")]) \%>\% + uColors( + low = "blue", + high = "red" + ) +}