htmlwidgets / sparkline

jQuery Sparkline HTML Widget for R
Other
245 stars 40 forks source link

Passing Pre-Computed Values to Boxplot Renders Incorrect Chart #36

Closed lyndess closed 9 months ago

lyndess commented 10 months ago

When creating a boxplot spark graph in R and manually specifying the boxplot parameters (as described here: https://omnipotent.net/jquery.sparkline/#boxplot), it doesn't produce the desired output. As a simple example:

library(sparkline)

x = rnorm(20)

sparkline(
  values = x,
  raw = TRUE,
  type = "box",

  # ignore data and manually specify parameters
  low_outlier = 412.000,
  low_whisker = 412.000,
  q1 = 444.750,
  median = 487.000,
  target = 487.140,
  q3 = 516.250,
  high_whisker = 592.000,
  high_outlier = 592.000,

  # format the boxplot
  outlierLineColor = "#15607a",
  outlierFillColor = "#b5f0fa",
  boxLineColor = "#15607a",
  boxFillColor = "#b5f0fa",
  whiskerColor = "#15607a",
  medianColor = "#15607a",
  targetColor = "#15607a",
  width = 400,
  height = 100
)

In the code above, I pass the 'x' data source to sparkline because it is required, but I want to manually specify the boxplot parameters—as indicated above. The resulting boxplot looks like this (including the hover tooltip):

raw=TRUE

Setting raw = FALSE results in this:

raw=FALSE

The expected result would be a boxplot with the specified parameters; neither setting produces the prescribed boxplot.

ArthurAndrews commented 9 months ago

Pre-computed box plot values (by, for example boxplot.stats) have to be sent to sparkline as the values argument. In other words, you won't send the vector of observations to sparkline at all. There's an example here: https://stackoverflow.com/questions/40123559/inline-boxplots-using-sparkline

lyndess commented 9 months ago

Ahh, OK—so "low_outlier", "low_whisker", "q1", etc. are actually not arguments to sparkline, but instead one must store these values in a vector that is passed as the values argument.

So my simple example code would be updated as follows:

library(sparkline)

low_outlier <- 382.000
low_whisker <- 412.000
q1 <- 444.750
median <- 487.000
target <- 487.140
q3 <- 516.250
high_whisker <- 592.000
high_outlier <- 605.000

# low_outlier, low_whisker, q1, median, q3, high_whisker, high_outlier
raw_values = c(low_outlier, low_whisker, q1, median, q3, high_whisker, high_outlier)

sparkline(
  values = raw_values,
  raw = TRUE,
  type = "box",

  # format the boxplot
  outlierLineColor = "#15607a",
  outlierFillColor = "#b5f0fa",
  boxLineColor = "#15607a",
  boxFillColor = "#b5f0fa",
  whiskerColor = "#15607a",
  medianColor = "#15607a",
  targetColor = "#15607a",
  width = 400,
  height = 100
)

... which correctly produces this (including the hover tip): image

Thank you for clarifying!

lyndess commented 9 months ago

Issue resolved.