library(nivor)
library(reactable)

data <- data.frame(
  index = rep(0:13, 6),
  x = rep(0:13, 6),
  y = c(
    2.0, 2.1, 2.3, 2.5, 2.7, 2.9, 3.0, 3.2, 3.3, 3.5, 3.6, 3.8, 3.9, 4.0,
    3.2, 3.3, 3.6, 3.8, 4.1, 4.3, 4.6, 4.8, 5.0, 5.2, 5.4, 5.5, 5.7, 5.8,
    4.8, 5.1, 5.4, 5.7, 6.1, 6.5, 6.8, 7.1, 7.3, 7.6, 7.8, 8.1, 8.3, 8.5,
    2.5, 2.6, 2.8, 3.1, 3.3, 3.5, 3.8, 4.0, 4.2, 4.4, 4.5, 4.7, 4.9, 5.0,
    3.3, 3.5, 3.8, 4.1, 4.4, 4.7, 4.9, 5.2, 5.4, 5.6, 5.8, 6.0, 6.2, 6.4,
    5.0, 5.3, 5.6, 6.0, 6.4, 6.8, 7.2, 7.5, 7.8, 8.0, 8.3, 8.5, 8.8, 9.0
  ),
  z = c(
    1, 2, 3, 1, 3, 0, 1, 3, 2, 3, 0, 0, 4, 1,
    1, 3, 4, 0, 1, 1, 2, 2, 3, 0, 1, 1, 2, 4,
    4, 3, 4, 1, 0, 1, 2, 3, 3, 1, 1, 0, 4, 1,
    3, 2, 0, 0, 1, 4, 4, 4, 3, 2, 1, 0, 0, 3,
    0, 4, 3, 3, 2, 2, 2, 1, 1, 0, 3, 4, 2, 1,
    4, 3, 2, 2, 1, 2, 1, 3, 4, 1, 0, 2, 1, 4
  ),
  group = c(
    rep("girls low", 14),
    rep("girls med", 14),
    rep("girls high", 14),
    rep("boys low", 14),
    rep("boys med", 14),
    rep("boys high", 14)
  )
)
reactable(data)
common_settings <- list(
  width = 800,
  height = 400,
  margin = list(
    top = 30,
    right = 30,
    bottom = 60,
    left = 80
  ),
  nodeSize = 10,
  blendMode = "multiply",
  xFormat = htmlwidgets::JS("d => `week ${d}`"),
  yFormat = htmlwidgets::JS("d => `${d} kg`"),
  axisBottom = list(
    format = htmlwidgets::JS("d => `week ${d}`")
  ),
  axisLeft = list(
    format = htmlwidgets::JS("d => `${d} kg`")
  ),
  legends = list(
    list(
      anchor = "bottom-left",
      direction = "row",
      translateY = 60,
      itemWidth = 130,
      itemHeight = 12,
      symbolSize = 12,
      symbolShape = "circle"
    )
  )
)

Default

In R, we use do.call to constructs and executes a function call from a name or a function and a list of arguments to be passed to it.

do.call(
  n_scatter,
  n_options(
    default = common_settings,
    data = data[data$group == "girls low", ],
    x = "x",
    y = "y",
    group = "group" # use group column for series name
  )
)

Multiple Series

do.call(
  n_scatter,
  n_options(
    default = common_settings,
    data = data,
    x = "x",
    y = "y",
    group = "group"
  )
)

Alternative colors

do.call(
  n_scatter,
  n_options(
    default = common_settings,
    data = data,
    x = "x",
    y = "y",
    group = "group",
    colors = list(
      scheme = "category10"
    )
  )
)

Using Time Scales

data <- data.frame(
  group = c(rep("apple", 8), rep("oranges", 8)),
  x = rep(seq.Date(as.Date("2018-01-01"), by = "days", length.out = 8), 2),
  y = c(7, 5, 11, 9, 12, 16, 13, 13, 14, 14, 15, 11, 10, 12, 9, 7)
)
reactable(data)
settings <- n_options(
  default = common_settings,
  data = data,
  x = "x",
  y = "y",
  group = "group",
  xScale = list(
    type = "time",
    format = "%Y-%m-%d",
    precision = "day"
  ),
  xFormat = "time:%Y-%m-%d",
  axisBottom = list(
    format = "%b %d",
    tickValues = "every 2 days"
  )
)

do.call(n_scatter, settings)

Using Logarithmic Scales

data <- data.frame(
  name = "apples",
  x = c(10, 100, 1000, 10000, 100000, 1000000),
  y = c(2, 4, 8, 16, 32, 64)
)

settings <- n_options(
  default = common_settings,
  data = data,
  x = "x",
  y = "y",
  group = "name",
  xScale = list(
    type = "log",
    base = 10
  ),
  xFormat = NA,
  yScale = list(
    type = "log",
    base = 2
  ),
  yFormat = NA,
  axisBottom = list(
    tickValues = c(10, 100, 1000, 1000, 10000, 100000, 1000000, 10000000)
  ),
  axisLeft = list(
    tickValues = c(2, 4, 8, 16, 32, 64)
  )
)

do.call(n_scatter, settings)