knitr::opts_chunk$set(echo = TRUE, warning=FALSE) suppressWarnings(suppressMessages(suppressPackageStartupMessages(library(ggplot2))))
Dotplots were made popular by the statistician W. S. Cleveland and are described in the book The Elements of Graphing Data.
The R core team implemented this function in the base graphics package as
the function stripchart. You can get help on this function by typing
the command ??stripchart.
There is a helpful YouTube video here.
This example is used data supplied from page 41 in your text Robert Johnson and Patricia Kuby.
Use the list of 19 exam grades from Illustration 2.3 on p. 41.
We will create a list named grades.
grades <- c(76, 74, 82, 96, 66, 76, 78, 72, 52, 68, 86, 84, 62, 76, 78, 92, 82, 74, 88)
First, use the base graphics function stripchart:
stripchart(grades, method="stack", at=c(0.05), pch=20, cex=3, las-1, frame.plot=FALSE, xlim=c(50,100), main="Exam grades")
We can also try the newer ggplot2 graphics.
df <- data.frame(grades=grades) a <- ggplot(df, aes(x = grades)) + geom_dotplot(binwidth=1) + scale_x_continuous(breaks=seq(from=50, to=100, by=5), limits = c(50, 100)) + scale_y_continuous(NULL, breaks = NULL) + xlab("grade") + theme(axis.text=element_text(size=12), axis.title=element_text(size=12), plot.title=element_text(hjust = 0.5)) print(a)
For comparison, let's look at the histogram using the base graphics function. We will set the breaks to have bins one grade wide. In general, histograms are more useful, especially for larger data sets.
minG <- min(grades)-1 maxG <- max(grades)+1 breaks <- seq(minG, maxG, 1) a <- hist(grades, breaks, xlim=c(minG, 100))
Now try the newer ggplot2 approach
a <- ggplot(df, aes(x = grades)) + geom_histogram(bins=50) + xlab("grade") + ggtitle("Exam grade distribution") + theme(axis.text=element_text(size=12), axis.title=element_text(size=12), plot.title=element_text(hjust = 0.5)) # center the title print(a)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.