Skip to content

Commit

Permalink
added uColors
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed Oct 11, 2023
1 parent e21eda6 commit 7bb28ff
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 13 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export("%>%")
export(renderUPlot)
export(uColors)
export(uPlot)
export(uPlotOutput)
export(uSeries)
Expand Down
30 changes: 30 additions & 0 deletions R/uSeries.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"))) {
Expand Down
23 changes: 11 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
<!-- badges: end -->


: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:
Expand All @@ -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)

Expand Down
29 changes: 29 additions & 0 deletions man/uColors.Rd

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

0 comments on commit 7bb28ff

Please sign in to comment.