Learn R Programming

echarty (version 1.4.7)

ecr.ebars: Error bars

Description

Custom series to display error-bars for scatter,bar or line series

Usage

ecr.ebars(wt, df = NULL, hwidth = 6, ...)

Value

A widget with error bars added if successful, otherwise the input wt

Arguments

wt

A widget to add error bars to, see createWidget

df

NULL(default) or data.frame with four or more columns ordered exactly x,y,low,high,(category),...
When NULL, data is taken from wt's dataset where order should be the same x,y,low,high,(category),...

hwidth

Half-width of error bar in pixels, default is 6.

...

More parameters for custom serie

Details

ecr.ebars are custom series, so ec.init(load='custom') is required.
Grouped series are supported, but df is required with group column included.
Will add a chart legend and its own tooltip if none is provided.
Add a name attribute to show error bars separate in the legend.
Command should be called last, after all other series.
Simple non-grouped series could be displayed with formatter riErrBarSimple instead of ecr.ebars. See example below.

Examples

Run this code
if (interactive()) {
library(dplyr)
tmp <- round(rnorm(24, sin(1:24/2)*10, .5))
df <- data.frame(x = 1:24, val = tmp, 
                 lower = round(rnorm(24, tmp -10, .5)),
                 upper = round(rnorm(24, tmp + 5, .8)),
                 cat= rep(c('A','B'),24) )
                 
df |> ec.init(load='custom', tooltip= list(show=TRUE)) |> ecr.ebars()
  
#------ riErrBarSimple ------
df |> ec.init(load= 'custom',
              title= list(text= "riErrBarSimple"),
              legend= list(show=TRUE),
              xAxis= list(data= df$category)) |> ec.upd({
  series <- append(series, list(list(
    type= "custom", name= "error",
    itemStyle= list(borderWidth= 1.5, color= 'brown'),
    encode= list(x= 0, y= list(1, 2)),
    data= ec.data(df |> select(x,lower,upper)),
    renderItem= htmlwidgets::JS("riErrBarSimple") )))
})

# ----- grouped -------
df |> group_by(cat) |> 
  ec.init(load= 'custom',
          xAxis= list(type='value')) |>  # fix preset 'category'
	 ecr.ebars(df)
}

Run the code above in your browser using DataLab