Skip to content

Commit

Permalink
added uScatter (x & y only)
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed Dec 4, 2024
1 parent e5ce549 commit 81dce44
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export(uBands)
export(uColors)
export(uPlot)
export(uPlotOutput)
export(uScatter)
export(uSeries)
importFrom(grDevices,palette)
importFrom(htmlwidgets,createWidget)
Expand Down
58 changes: 58 additions & 0 deletions R/uScatter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

#' Create a scatter chart
#'
#' @param x Numeric vector to use on x-axis.
#' @param y Numeric vector to use on y-axis.
#' @param color A vector to identify different data series.
#' @param default_stroke Default stroke color to use if `color` is `NULL`.
#' @param label_x,label_y Labels for x and y axes.
#' @inheritParams uPlot
#'
#' @return An `htmlwidget` object of class `"uPlot"`.
#' @export
#'
#' @example examples/ex-uScatter.R
uScatter <- function(x,
y,
color = NULL,
default_stroke = "black",
label_x = NULL,
label_y = NULL,
options = list(),
use_gzipped_json = FALSE,
width = NULL,
height = NULL,
elementId = NULL) {
data <- if (is.null(color)) {
list(
NULL,
list(x, y)
)
}
if (is.null(label_x))
label_x <- deparse(substitute(x))
if (is.null(label_y))
label_y <- deparse(substitute(y))
if (is.null(options$axes))
options$axes <- list(list(), list())
if (is.null(options$axes[[1]]$label))
options$axes[[1]]$label <- label_x
if (is.null(options$axes[[2]]$label))
options$axes[[2]]$label <- label_y
options$mode <- 2
options$legend$show <- FALSE
options$scales$x$time <- FALSE
options$scales$y <- list()
options$series[[2]]$paths <- htmlwidgets::JS("drawPoints")
if (is.null(options$series[[2]]$stroke))
options$series[[2]]$stroke <- default_stroke
options$series[[1]] <- list()
uPlot(
data = data,
options = options,
use_gzipped_json = use_gzipped_json,
width = width,
height = height,
elementId = elementId
)
}
4 changes: 4 additions & 0 deletions examples/ex-uScatter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

library(uPlot)

uScatter(cars$speed, cars$dist)
3 changes: 3 additions & 0 deletions examples/scatter.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

uScatter(ggplot2::diamonds$carat, ggplot2::diamonds$price)


uPlot::uPlot(
data = list(
NULL,
Expand Down
51 changes: 51 additions & 0 deletions man/uScatter.Rd

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

0 comments on commit 81dce44

Please sign in to comment.