Skip to content

Commit

Permalink
fixed double legend in scale_gradient + example
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed Sep 23, 2024
1 parent cb16249 commit e041f0f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 11 deletions.
16 changes: 5 additions & 11 deletions R/scales.R
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ v_scale_fill_discrete <- v_scale_color_discrete
#'
#' @name scale-gradient
#'
# @examples
#' @example examples/scale_gradient.R
v_scale_colour_gradient <- function(vc,
name = NULL,
low = "#132B43",
Expand Down Expand Up @@ -929,11 +929,7 @@ v_scale_gradient <- function(vc,
"'vc' must be a 'vchart' htmlwidget object" = inherits(vc, "vchart")
)

if (!is.null(vc$x$mapdata[[aesthetic]])) {
x <- vc$x$mapdata[[aesthetic]]
} else {
x <- unlist(lapply(vc$x$mapdata, `[[`, aesthetic))
}
x <- get_aes_data(extract_data(vc), c(aesthetic))

if (is.null(limits))
limits <- range(pretty(range(x, na.rm = TRUE)))
Expand All @@ -955,6 +951,8 @@ v_scale_gradient <- function(vc,
range = c(low, high)
)
)
i <- vapply(vc$x$specs$legends, function(x) identical(x$type, "color"), logical(1))
vc$x$specs$legends[i] <- NULL
vc <- .vchart_specs(
vc, "legends",
list(dropNulls(list(
Expand Down Expand Up @@ -1001,11 +999,7 @@ v_scale_size <- function(vc,
"'vc' must be a 'vchart' htmlwidget object" = inherits(vc, "vchart")
)

if (!is.null(vc$x$mapdata$size)) {
x <- vc$x$mapdata$size
} else {
x <- unlist(lapply(vc$x$mapdata, `[[`, "size"))
}
x <- get_aes_data(extract_data(vc), "size")


title <- if (is.character(name) & length(name) == 1) {
Expand Down
2 changes: 2 additions & 0 deletions R/shiny.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#' @export
#'
#' @importFrom htmlwidgets shinyWidgetOutput
#'
#' @return An output or render function that enables the use of the widget within Shiny applications.
#'
#' @example examples/shiny.R
vchartOutput <- function(outputId, width = "100%", height = "400px"){
Expand Down
15 changes: 15 additions & 0 deletions examples/scale_gradient.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

library(vchartr)
data("penguins", package = "palmerpenguins")

vchart(penguins) %>%
v_scatter(aes(
x = bill_length_mm,
y = bill_depth_mm,
color = body_mass_g
)) %>%
v_scale_colour_gradient(
name = "Body mass",
low = "yellow",
high = "red"
)
17 changes: 17 additions & 0 deletions man/scale-gradient.Rd

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

3 changes: 3 additions & 0 deletions man/vchart-shiny.Rd

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

0 comments on commit e041f0f

Please sign in to comment.