Table of Contents
To create a Stacked Dot Plot in R, you can use the function geom_dotplot from the ggplot2 package. This function takes a data frame as an argument and allows you to specify the variables to be plotted and the stacking order. You can then add additional plot aesthetics like size, color, and transparency to customize the plot.
A stacked dot plot is a type of plot that displays frequencies using dots.
There are two methods you can use to create a stacked dot plot in R:
Method 1: The function in base R.
Method 2: The function in ggplot2.
This tutorial provides a brief example of how to use each of these methods to produce a stacked dot plot.
Example 1: Stacked Dot Plot in Base R
The following code shows how to make a basic stacked dot plot in base R:
#create some fake data set.seed(0) data <- sample(0:20, 100, replace = TRUE) #create stacked dot plot stripchart(data, method = "stack")
And the following code shows how to customize the stacked dot plot to make it more aesthetically pleasing:
#create some fake data set.seed(0) data <- sample(0:20, 100, replace = TRUE) #create stacked dot plot stripchart(data, method = "stack", offset = .5, at = 0, pch = 19, col = "steelblue", main = "Stacked Dot Plot", xlab = "Data Values")
Example 2: Stacked Dot Plot in ggplot2
The following code shows how to make a basic stacked dot plot in ggplot2:
#load ggplot2 library(ggplot2) #create some fake data set.seed(0) data <- data.frame(x = sample(0:20, 100, replace = TRUE)) #create stacked dot plot ggplot(data, aes(x = x)) + geom_dotplot()
And the following code shows how to customize the stacked dot plot to make it more aesthetically pleasing:
#load ggplot2 library(ggplot2) #create some fake data set.seed(0) data <- data.frame(x = sample(0:20, 100, replace = TRUE)) #create customized stacked dot plot ggplot(data, aes(x = x)) + geom_dotplot(dotsize = .75, stackratio = 1.2, fill = "steelblue") + scale_y_continuous(NULL, breaks = NULL) + labs(title = "Stacked Dot Plot", x = "Data Values", y = "")
You can find more R tutorials .