# Creating Stacked Dot Plots in R: A Guide with Base R and ggplot2

**Steve's Data Tips and Tricks**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

# Introduction

Stacked dot plots are a type of plot that displays frequencies using dots, piled one over the other. In R, there are several ways to create stacked dot plots, including using base R and ggplot2. In this blog post, we will explore how to create stacked dot plots in both Base R and ggplot2, and provide several examples of each.

# Examples Stacked Dot Plots in Base R

## Method 1: The stripchart() function

The stripchart() function in base R can be used to create a basic stacked dot plot. Here is an example of how to use it:

# create some fake data set.seed(0) data <- sample(0:20, 100, replace = TRUE) # create stacked dot plot stripchart(data, method = "stack")

This will create a basic stacked dot plot. However, we can customize it to make it more aesthetically pleasing. Here is an example of how to do that:

# 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")

This will create a stacked dot plot with a blue color scheme and a more aesthetically pleasing layout.

## Method 2: The dotchart() function

Another way to create a stacked dot plot in base R is to use the dotchart() function. Here is an example of how to use it:

# create some fake data set.seed(0) data <- sample(0:20, 100, replace = TRUE) # create stacked dot plot dotchart(data, cex = .7, col = "steelblue", main = "Stacked Dot Plot", xlab = "Data Values")

This will create a stacked dot plot with a blue color scheme and a more aesthetically pleasing layout.

# Examples Stacked Dot Plots in ggplot2

## Method 1: The geom_dotplot() function

The geom_dotplot() function in ggplot2 can be used to create a basic stacked dot plot. Here is an example of how to use it:

# 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() + theme_minimal()

Bin width defaults to 1/30 of the range of the data. Pick better value with `binwidth`.

This will create a basic stacked dot plot. However, we can customize it to make it more aesthetically pleasing. Here is an example of how to do that:

# 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") + theme_minimal()

This will create a stacked dot plot with a blue color scheme and a more aesthetically pleasing layout.

## Method 2: The geom_jitter() function

Another way to create a stacked dot plot in ggplot2 is to use the geom_jitter() function. Here is an example of how to use it:

# 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, y = 0)) + geom_jitter(height = .1, width = 0, alpha = .5, color = "steelblue") + labs(title = "Stacked Dot Plot", x = "Data Values") + theme_minimal()

In conclusion, creating stacked dot plots in R is a simple and effective way to visualize frequency data. By using either base R or ggplot2, you can create aesthetically pleasing plots that are easy to interpret. We encourage readers to try creating their own stacked dot plots using the examples provided in this blog post.

# ReferencesCitations:

- [1] https://www.statology.org/stacked-dot-plot-in-r/
- [2] https://www.geeksforgeeks.org/how-to-create-a-stacked-dot-plot-in-r/
- [3] http://www.sthda.com/english/wiki/ggplot2-dot-plot-quick-start-guide-r-software-and-data-visualization
- [4] https://stackoverflow.com/questions/61471353/in-r-ggplot-how-do-i-stack-two-dotplots
- [5] https://ggplot2.tidyverse.org/reference/geom_dotplot.html
- [6] https://eurekastatistics.com/r-flavours-of-stacked-dotplots/

**leave a comment**for the author, please follow the link and comment on their blog:

**Steve's Data Tips and Tricks**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.