stackedBar: Adjust (stacked) bar chart to values on a given scale

Description Usage Arguments Details Value Examples

View source: R/svgtools.R

Description

Adjusts the horizontal (XML attribute 'x') or vertical (XML attribute 'y') position as well as width/height of bar segments (XML elements of type 'rect') and optionally value labels (XML elements of type 'text'). Positions are calculated relative to a given frame (XML element of type 'rect') and the position of a data value within the minimum and maximum of a given scale. This process is called scaling.
In preparation, it is necessary to name a group (set attribute 'id' of XML element of type 'g') of bar segments (and value labels). Bar segments (and value labels) need to be of the same amount as there are data values for adjustment.
It is possible to group several such groups together. Only the outer group needs to be named in that case, for convenience.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
stackedBar(
  svg,
  frame_name,
  group_name,
  scale_real,
  values,
  alignment = "horizontal",
  has_labels = TRUE,
  label_position = "center",
  decimals = 0,
  display_limits = 0,
  ...
)

Arguments

svg

XML document with SVG content

frame_name

Name (attribute 'id') of frame (XML element 'rect') for positioning bar segments (and value labels).

group_name

Name (attribute 'id') of group (XML element 'g') containing either bar segments (and value labels) or further groups, containing bar segments (and value labels) themselves.

scale_real

Numeric vector (e.g. c(0,100)) of arbitrary length. Only minimum and maximum are used for scaling of values.

values

Either a numeric vector, a numeric matrix or a dataframe with only numeric columns.
If a vector is given, it corresponds to one bar (group of bar segments and, optionally, value labels).
If a matrix or dataframe is given, rows define the value set for several (stacked) bars grouped together.

alignment

Character value. Accepts 'horizontal' (default) or 'vertical'. See details.

has_labels

Are there value labels (of XML type 'text') to adjust? (default TRUE)

label_position

Character value. Accepts 'start', 'center' (default) and 'end'. This refers to the underlying bar segments.

decimals

Integer value defining the number of decimal digits of value labels (default 0).

display_limits

Interval for (small) values, that lead to suppression of the corresponding value labels. If only one value x is given, it is turned into the interval c(-x,x). (default 0 = no suppression)

...

Further arguments used internally by referenceBar, diffBar and percentileBar.

Details

'Horizontal' alignment refers to adjustment of the x-coordinates of elements, 'vertical' alignment to adjustment of the y-coordinates.
Bar segments and, optionally, value labels may be grouped together in any order in the SVG file. The function will automatically use XML elements from left to right (with alignment='horizontal') or bottom to top (with alignment='vertical') according to their x/y-coordinates.
Furthermore, the SVG file order of several bars grouped together in an outer group is irrelevant. The function will automatically use bars (that is, groups of bar segments and, optionally, value labels) from top to bottom (with alignment='horizontal') or left to right (with alignment='vertical') according to the lowest x/y-coordinate of any element.
Bar segments and value labels (if any) are automatically hidden (XML attribute 'diplay' is set to 'none'), when a value of 0 or NA is provided. Subsequent calls to the function with non-zero or non-NA values make such elements reappear in the output.

Value

XML document with SVG content

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#read SVG file
fpath <- system.file("extdata", "fig3.svg", package="svgtools")
svg <- read_svg(file = fpath)

#adjust bars
svg <- stackedBar(svg = svg, frame_name = "frame", group_name = "overall",
                  scale_real = c(0,160), values = c(9.97,42.42,105.71), 
                  alignment = "vertical", has_labels = TRUE, 
                  label_position = "end", decimals = 0, display_limits = 10)
df.subgroups <- matrix(1:9*8, nrow=3)
svg <- stackedBar(svg = svg, frame_name = "frame", group_name = "subgroups", 
                  scale_real = c(0,160), values = df.subgroups, 
                  alignment = "vertical", display_limits = 10)

Example output



svgtools documentation built on Dec. 14, 2021, 5:10 p.m.