dotchart
Cleveland's Dot Plots
Draw a Cleveland dot plot.
- Keywords
- hplot
Usage
dotchart(x, labels = NULL, groups = NULL, gdata = NULL,
ann = par("ann"), xaxt = par("xaxt"), frame.plot = TRUE, log = "",
cex = par("cex"), pt.cex = cex,
pch = 21, gpch = 21, bg = par("bg"),
color = par("fg"), gcolor = par("fg"), lcolor = "gray",
xlim = range(x[is.finite(x)]),
main = NULL, xlab = NULL, ylab = NULL, …)
Arguments
- x
either a vector or matrix of numeric values (
NA
s are allowed). Ifx
is a matrix the overall plot consists of juxtaposed dotplots for each row. Inputs which satisfyis.numeric(x)
but notis.vector(x) || is.matrix(x)
are coerced byas.numeric
, with a warning.- labels
a vector of labels for each point. For vectors the default is to use
names(x)
and for matrices the row labelsdimnames(x)[[1]]
.- groups
an optional factor indicating how the elements of
x
are grouped. Ifx
is a matrix,groups
will default to the columns ofx
.- gdata
data values for the groups. This is typically a summary such as the median or mean of each group.
- ann
a
logical
value indicating whether the default annotation (title and x and y axis labels) should appear on the plot.- xaxt
a string indicating the x-axis style; use
"n"
to suppress and see alsopar("xaxt")
.- frame.plot
a logical indicating whether a box should be drawn around the plot.
- log
a character string indicating if one or the other axis should be logarithmic, see
plot.default
.- cex
the character size to be used. Setting
cex
to a value smaller than one can be a useful way of avoiding label overlap. Unlike many other graphics functions, this sets the actual size, not a multiple ofpar("cex")
.- pt.cex
the
cex
to be applied to plotting symbols. This behaves likecex
inplot()
.- pch
the plotting character or symbol to be used.
- gpch
the plotting character or symbol to be used for group values.
- bg
the background color of plotting characters or symbols to be used; use
par(bg= *)
to set the background color of the whole plot.- color
the color(s) to be used for points and labels.
- gcolor
the single color to be used for group labels and values.
- lcolor
the color(s) to be used for the horizontal lines.
- xlim
horizontal range for the plot, see
plot.window
, for example.- main
overall title for the plot, see
title
.- xlab, ylab
axis annotations as in
title
.- …
graphical parameters can also be specified as arguments.
Value
This function is invoked for its side effect, which is to produce two variants of dotplots as described in Cleveland (1985).
Dot plots are a reasonable substitute for bar plots.
References
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
Cleveland, W. S. (1985) The Elements of Graphing Data. Monterey, CA: Wadsworth.
Murrell, P. (2005) R Graphics. Chapman & Hall/CRC Press.
Examples
library(graphics)
# NOT RUN {
dotchart(VADeaths, main = "Death Rates in Virginia - 1940")
op <- par(xaxs = "i") # 0 -- 100%
dotchart(t(VADeaths), xlim = c(0,100), bg = "skyblue",
main = "Death Rates in Virginia - 1940")
par(op)
# }
Community examples
[Example file for LinkedIn Learning video](https://linkedin-learning.pxf.io/rweekly_dotchart) ```r # dotchart works with matrix or vector (not dataframe) # Dotplot for vectors ---- vectorToPlot <- c(1:6) names(vectorToPlot) <- c(LETTERS[1:6]) dotchart(vectorToPlot, cex = .7) myGroup <- factor(c("group1","group3","group2","group1","group3","group2")) dotchart(vectorToPlot, groups = myGroup) dotchart(vectorToPlot, gcolor = "red", groups = myGroup, gdata = c(median(vectorToPlot[myGroup == "group1"]), median(vectorToPlot[myGroup == "group2"]), median(vectorToPlot[myGroup == "group3"])), cex = .7, main = "Groups of Things", xlab = "Things") # dotplot for matrix ---- View(WorldPhones) str(WorldPhones) # worldphones is a matrix - not a dataframe # Major labels (Groups) are matrix columns. Minor labels are matrix rows dotchart(WorldPhones) # works, but it's messy dotchart(WorldPhones, gcolor = "Blue", cex = .5, gdata = colMeans(WorldPhones), gpch = 15, main = "World Phones by Country") ```