Dotplots are used to display the distribution of a single quantitative variable. They are usually used for small data sets, 30 observations or less. The number of bins is calculated automatically, but you should always pick the number of bins that best displays the distribution of the data. You can either set the number of bins to be used with the bins
argument, or you can set the width of the bins by using the binwidth
argument. Bins are the intervals that cover the x axis. Each dot in the dotplot is sitting on a bin.
The code below generates a dotplot of gas mileage for the mtcars data set with the default binwidth and color. If you do not supply the number of bins
or a binwidth
an error message is generated along with the graph.
library(ggplot2)
ggplot(data = mtcars, aes(x = mpg)) +
geom_dotplot() +
ggtitle("Distribution of Gass Mileage") +
xlab("Miles per Gallon")
## `stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.
Here the binwidth
and fill
arguments are used to generate a dotplot with the desired specifications.
ggplot(data = mtcars, aes(x = mpg)) +
geom_dotplot(binwidth = 2, fill = "violet") +
ggtitle("Distribution of Gass Mileage") +
xlab("Miles per Gallon")
The code below generates separate dotplots of gas mileage for cars based on the number of cylinders.
library(ggplot2)
mtcars$cyl <- factor(mtcars$cyl)
ggplot(data = mtcars, aes(x = mpg)) +
geom_dotplot(binwidth = 2) +
ggtitle("Distribution of Gass Mileage by Cylinders") +
xlab("Miles per Gallon") +
ylab("") +
facet_grid(. ~ cyl)
Mathematicss, Computer Science, and Statistics Department Gustavus Adolphus College