strip charts (i.e., one dimensional scatter plots or dot plots) in R. These plots are a good alternative to box plots when sample sizes are small.
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
# Print the first 6 rows
head(ToothGrowth, 6)
## len supp dose
## 1 4.2 VC 0.5
## 2 11.5 VC 0.5
## 3 7.3 VC 0.5
## 4 5.8 VC 0.5
## 5 6.4 VC 0.5
## 6 10.0 VC 0.5
stripchart(x, data = NULL method = “overplot”, jitter = 0.1)
x: the data from which the plots are to be produced. Allowed values are one or a list of numeric vector, each corresponding to a component plot. data: a data.frame (or list) from which the variables in x should be taken. Method: the method to be used to separate coincident points. Allowed values are one of “overplot”, “jitter” or “stack”. jitter: when method = “jitter” is used, jitter gives the amount of jittering applied.
# Plot len by dose
stripchart(len ~ dose, data = ToothGrowth,
pch = 19, frame = FALSE)
# Vertical plot using method = "jitter"
stripchart(len ~ dose, data = ToothGrowth,
pch = 19, frame = FALSE, vertical = TRUE,
method = "jitter")
# Change point shapes (pch) and colors by groups
# add main title and axis labels
stripchart(len ~ dose, data = ToothGrowth,
frame = FALSE, vertical = TRUE,
method = "jitter", pch = c(21, 18, 16),
col = c("#999999", "#E69F00", "#56B4E9"),
main = "Length by dose", xlab = "Dose", ylab = "Length")
Documentation Examples
stripchart(x, …) # S3 method for formula stripchart(x, data = NULL, dlab = NULL, …, subset, na.action = NULL)
stripchart(x, method = “overplot”, jitter = 0.1, offset = 1/3, vertical = FALSE, group.names, add = FALSE, at = NULL, xlim = NULL, ylim = NULL, ylab = NULL, xlab = NULL, dlab = "“, glab =”“, log =”“, pch = 0, col = par(”fg“), cex = par(”cex"), axes = TRUE, frame.plot = axes, …)
Arguments x the data from which the plots are to be produced. In the default method the data can be specified as a single numeric vector, or as list of numeric vectors, each corresponding to a component plot. In the formula method, a symbolic specification of the form y ~ g can be given, indicating the observations in the vector y are to be grouped according to the levels of the factor g. NAs are allowed in the data.
data a data.frame (or list) from which the variables in x should be taken.
subset an optional vector specifying a subset of observations to be used for plotting.
na.action a function which indicates what should happen when the data contain NAs. The default is to ignore missing values in either the response or the group.
… additional parameters passed to the default method, or by it to plot.window, points, axis and title to control the appearance of the plot.
method the method to be used to separate coincident points. The default method “overplot” causes such points to be overplotted, but it is also possible to specify “jitter” to jitter the points, or “stack” have coincident points stacked. The last method only makes sense for very granular data.
jitter when method = “jitter” is used, jitter gives the amount of jittering applied.
offset when stacking is used, points are stacked this many line-heights (symbol widths) apart.
vertical when vertical is TRUE the plots are drawn vertically rather than the default horizontal.
group.names group labels which will be printed alongside (or underneath) each plot.
add logical, if true add the chart to the current plot.
at numeric vector giving the locations where the charts should be drawn, particularly when add = TRUE; defaults to 1:n where n is the number of boxes.
ylab, xlab labels: see title.
dlab, glab alternate way to specify axis labels: see ‘Details’.
xlim, ylim plot limits: see plot.window.
log on which axes to use a log scale: see plot.default
pch, col, cex Graphical parameters: see par.
axes, frame.plot Axis control: see plot.default.
x <- stats::rnorm(50)
xr <- round(x, 1)
stripchart(x) ; m <- mean(par("usr")[1:2])
text(m, 1.04, "stripchart(x, \"overplot\")")
stripchart(xr, method = "stack", add = TRUE, at = 1.2)
text(m, 1.35, "stripchart(round(x,1), \"stack\")")
stripchart(xr, method = "jitter", add = TRUE, at = 0.7)
text(m, 0.85, "stripchart(round(x,1), \"jitter\")")
Example 2
stripchart(decrease ~ treatment,
main = "stripchart(OrchardSprays)",
vertical = TRUE, log = "y", data = OrchardSprays)
Example 3
stripchart(decrease ~ treatment, at = c(1:8)^2,
main = "stripchart(OrchardSprays)",
vertical = TRUE, log = "y", data = OrchardSprays)