lollipop: Lollipop chart

View source: R/lollipop.R

lollipopR Documentation

Lollipop chart

Description

This function creates a lollipop chart (by default), a points chart (if sticks = FALSE) or a point-range chart (if sticks are confidence limits) from a (named) numeric vector.

Usage

  lollipop(x, names = NULL, ymin = 0, ylim = "auto0", sticks = TRUE, 
  col = "royalblue4", grid = TRUE, cex = 1, cex.axis = 1, las = 2, 
  horiz = FALSE, bold = FALSE, ...)

Arguments

x

a numeric vector.

names

a vector of the same length as 'x' with the names to be plotted below the lollipops. If this argument is left NULL and 'x' has names, then these will be used.

ymin

numeric value for the lower limit of the y axis (default 0). If set to NA, the minimum of 'x' will be used. This argument is deprecated, as it is now superseded by, and should be replaced with, 'ylim' (below).

ylim

either a numeric vector of length 2 specifying the limits (minimum, maximum) for the y axis; or "auto" to fit the y axis to the minimum and maximum existing values in 'x'; or "auto0" (the new default) to fit the top of the y axis to the maximum existing values, and the bottom to zero or the minimum existing value (whichever is lower). If horiz is TRUE, these limits refer instead to the x axis.

sticks

either a logical value indicating whether the sticks of the lollipops (lines from zero to each value) should be drawn (default TRUE); or a two-column matrix or dataframe with one row for each element in x, with the minimum and maximum values for the error bars or confidence intervals (in which case a point-range chart is drawn).

col

colour for the lollipops.

grid

logical value (default TRUE) indicating whether or not to add a grid to the plot.

cex

numeric value indicating the size of the lollipops. Will be passed as 'cex' to points and as 'lwd' to arrows (the lines or lollipop sticks).

cex.axis

numeric value to pass to axis and to mtext indicating the size of the x and y axis labels.

las

argument to pass to par indicating the orientation of the axis labels.

horiz

logical value (default FALSE) indicating whether the plot should be drawn horizontally rather than vertically.

bold

logical vector (recycled if shorter than x, default FALSE) indicating which name labels should appear in bold font. Can be used e.g. to highlight significant values (see e.g. plotCoeffs).

...

additional arguments that can be passed to plot, e.g. 'main', 'xlab' or 'ylab'.

Details

According to modern data viz recommendations, lollipop charts (or instead point charts) are generally a better alternative to bar charts, as they reduce the visual distortion caused by the length of the bars, making it easier to visually compare the values.

Value

A plot.

Author(s)

A. Marcia Barbosa

See Also

plotCoeffs; barplot; ggplot2::geom_pointrange()

Examples

par(mar = c(6, 3, 2, 1))

lollipop(mtcars[,1], names = rownames(mtcars), las = 2, 
ylab = names(mtcars)[1], cex.axis = 0.6)

lollipop(mtcars[,1], names = rownames(mtcars), las = 2, 
ylab = names(mtcars)[1], cex.axis = 0.6, ylim = NA)


par(mar = c(3, 6, 2, 1))

lollipop(mtcars[,1], names = rownames(mtcars), las = 2, 
xlab = "Miles/gallon", cex.axis = 0.6, main = "Lollipop chart of mtcars", 
horiz = TRUE)

modEvA documentation built on March 20, 2025, 3:01 a.m.