lc_bars
creates a new barplot and adds it to the app and all currently opened pages
as a new chart or a new layer of an existing chart.
lc_bars(
data = list(),
place = NULL,
...,
chartId = NULL,
layerId = NULL,
with = NULL,
addLayer = FALSE
)
Name-value pairs of properties passed through the dat
function. These
properties will be re-evaluated on each updateCharts
call.
An ID of the container, where to place new chart. It will be ignored if the chart already exists. If not defined, the chart will be appended to the web page's body.
Name-value pairs of properties that will be evaluated only once and then will remain
constant. These properties can still be changed later using the setProperties
function.
An ID for the chart. All charts must have unique IDs. If a chart with the same ID already
exists, it will be replaced unless addLayer = TRUE
. If ID is not defined, it will be the same as the
value of the place
argument. And if both are not defined, the ID will be set to ChartN
,
where N - 1
is the number of existing charts.
An ID for the new layer. All layers within one chart must have different IDs. If a layer with the same
ID already exists, it will be replaced. If not defined, it will be set to LayerN
, where N - 1
is the current number of layers in this chart.
A dataset or a list from which other properties should be taken. If the dataset doesn't have a
column with the requested name, the variable will be searched for outside of the dataset. Must be
a data.frame
or a list
.
If there is already a chart with the same ID, this argument defines whether to replace it or to add a
new layer to it. This argument is ignored if both place
and chartId
are NULL
or if there is no
chart with the given ID.
You can read more about different properties here.
values
- heights of bars/stacks.
stackIds
- IDs of all stacks (optional). Must be the same size as values
.
barIds
- IDs of all bars (optional). Must be the same size as values
.
groupIds
- IDs of all groups (optional). Must be the same size as values
.
groupWidth
- a ratio of the width of a group of bars to the space available to the group.
Style settings
opacity
- a vector of opacity values for each bar or stack in the range from 0 to 1.
colour
- a vector of colours for each bar or stack. Must be a colour name or a hexadecimal code.
colourValue
- grouping values for different colours. Can be numbers or characters.
colourDomain
- a vector of all possible values for discrete colour scales
or a range of all possible colour values for the continuous ones.
palette
- a vector of colours to construct the colour scale.
colourLegendTitle
- a title for the colour legend.
addColourScaleToLegend
- whether or not to show the colour legend for the current layer.
globalColourScale
- whether or not to use one colour scale for all the layers.
stroke
- a vector of stroke colours for each bar or stack. Must be a colour name or a hexadecimal code.
strokeWidth
- a vector of stroke widths for each bar or stack.
Axes settings
logScaleX, logScaleY
- a base of logarithm for logarithmic scale transformation.
If 0 or FALSE
no transformation will be performed.
layerDomainX, layerDomainY
- default axes ranges for the given layer.
domainX, domainY
- default axes ranges for the entire chart. If not defined,
it is automatically set to include all layer domains.
contScaleX, contScaleY
- whether or not the axis should be continuous.
aspectRatio
- an aspect ratio for the chart.
axisTitleX, axisTitleY
- axis titles.
axisTitlePosX, axisTitlePosY
- positions of the axis titles. For each axis, one can specify a title position
across or along the corresponding axis. Possible options are "up"
(for title inside the plotting area)
or "down"
(outside the plotting area, under the axis), and
"start"
, "middle"
, "end"
. This property must be a string with one or two of the aforementioned options
(e.g. "middle down"
, "start"
, etc.).
ticksRotateX, ticksRotateY
- angles by which to rotate ticks (in degrees). Must be between
0 (horizontal ticks, default) and 90 (vertical ticks).
ticksX, ticksY
- sets of ticks for the axes.
Interactivity settings
on_click
- a function, to be called when one of the bars is clicked. Gets an
index of the clicked bar as an argument.
on_clickPosition
- a function, to be called when any point of the chart is clicked. Unlike
on_click
, which is called only when an element of the chart (point, line, etc.) is clicked, this
function reacts to any click on the chart. As an argument, it receives a vector of x and y coordinates of
the click (based on the current axes scales). If one of the axes is categorical, the function will
get the closest tick to the clicked position.
on_mouseover
- a function, to be called when the mouse hovers over one of the bars.
Gets an index of the clicked bar as an argument.
on_mouseout
- a function, to be called when the mouse moves out of one of the bars.
on_marked
- a function, to be called when any of the bars are selected (marked)
or deselected. Use getMarked
function to get the IDs of the currently marked bars. To mark bars,
select them with your mouse while holding the Shift key.
Legend settings
legend_width
- width of the legend in pixels. The default value is 200.
legend_height
- height of the legend in pixels. By default, it is equal to the height of the chart.
legend_sampleHeight
- height of a single key of the legend in pixels. The default value is 20.
legend_ncol
- number of columns to order several legends. By default, this is defined from the number
of legends to reach close to a square shape.
legend_container
- a DOM element of the web page where to place the legend. By default, the legend is
positioned to the right from the chart in a table cell specifically made for it. This should be a valid CSS selector.
If the specified element does not exist, the legend will be added to the web page's body.
Global chart settings
width
- width of the chart in pixels.
heigth
- height of the chart in pixels.
plotWidth
- width of the plotting area in pixels.
plotHeight
- height of the plotting area in pixels.
paddings
- padding sizes in pixels. Must be a list with all the following fields:
"top", "bottom", "left", "right"
.
title
- a title of the chart.
titleX, titleY
- coordinates of the chart title.
titleSize
- font-size of the chart title.
showLegend
- whether or not to show the legend.
showPanel
- whether of not to show the instrument panel (grey triangle in the upper-left corner of the chart).
transitionDuration
- duration of the transitions between any two states of the chart. If 0,
no animated transition is shown. It can be useful to turn the transition off, when lots of frequent
changes happen to the chart.
if (FALSE) data("esoph")
lc_bars(dat(value = tapply(esoph$ncases, esoph$agegp, sum),
title = "Number of cases per age group",
axisTitleX = "Age group",
axisTitleY = "Number of esophageal cases",
axisTitlePosX = "down"))
lc_bars(dat(value = c(tapply(esoph$ncases, esoph$agegp, sum),
tapply(esoph$ncontrols, esoph$agegp, sum)),
stackIds = c(rep("case", 6), rep("control", 6))))
#It is easy to put data in a convenient form for barplots using tidyverse
library(magrittr)
library(dplyr)
library(tidyr)
library(stringr)
esoph %>%
gather(type, cases, (ncases:ncontrols)) %>%
mutate(type = str_sub(type, 2, -2)) %>%
group_by(agegp, alcgp, type) %>%
summarise(ncases = sum(cases)) -> newData
lc_bars(dat(value = newData$ncases,
stackIds = newData$type,
barIds = newData$alcgp,
groupIds = newData$agegp))
Run the code above in your browser using DataLab