4.2 C
London
Friday, December 20, 2024
HomeStatistics TutorialRHow to Create a Stacked Dot Plot in R

How to Create a Stacked Dot Plot in R

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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 stripchart() function in base R.

Method 2: The geom_dotplot() 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 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 TRUE)

#create stacked dot plot
stripchart(data, method = "stack", offset = .5, at = 0, pch = 19,
           col = "steelblue", main = "Stacked Dot Plot", xlab = "Data Values")

Stacked dot plot in R

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 frame(x = sample(0:20, 100, replace = TRUE))

#create stacked dot plot
ggplot(data, aes(x = x)) +
  geom_dotplot()

Dot plot with ggplot2 in R

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

Stacked dot plot in ggplot2 with custom colors

You can find more R tutorials here.

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories