A Dot Plot is used to visualize the distribution of the data. This chart creates stacked dots, where each dot represents one observation.
Summary statistics are usually added to dotplots for indicating, for example, the median of the data and the interquartile range.
This article describes how to create and customize Dot Plots using the ggplot2 R package.
Related BookGGPlot2 Essentials for Great Data Visualization in R
Key R functions
- Key function:
geom_dotplot(). Creates stacked dots, with each dot representing one observation.
- Key arguments:
stackdir: which direction to stack the dots. “up” (default), “down”, “center”, “centerwhole” (centered, but with dots aligned).
stackratio: how close to stack the dots. Default is 1, where dots just just touch. Use smaller values for closer, overlapping dots.
fill: Dot border color and area fill
dotsize: The diameter of the dots relative to binwidth, default 1.
- Demo dataset:
- Continuous variable:
len(tooth length). Used on y-axis
- Grouping variable:
dose(dose levels of vitamin C: 0.5, 1, and 2 mg/day). Used on x-axis.
- Continuous variable:
First, convert the variable
dose from a numeric to a discrete factor variable:
data("ToothGrowth") ToothGrowth$dose <- as.factor(ToothGrowth$dose) head(ToothGrowth, 4)
## len supp dose ## 1 4.2 VC 0.5 ## 2 11.5 VC 0.5 ## 3 7.3 VC 0.5 ## 4 5.8 VC 0.5
Loading required R package
Load the ggplot2 package and set the default theme to
theme_classic() with the legend at the top of the plot:
library(ggplot2) theme_set( theme_classic() + theme(legend.position = "top") )
Basic Dot Plots
We start by initiating a plot named
e, then we’ll add layers. The following R code creates dotplots combined with summary statistics (mean +/- SD), boxplots and violin plots.
# Initiate a ggplot e <- ggplot(ToothGrowth, aes(x = dose, y = len)) # Dotplot with summary statistics: mean +/- SD e + geom_dotplot(binaxis = "y", stackdir = "center", fill = "lightgray") + stat_summary(fun.data = "mean_sdl", fun.args = list(mult=1)) # dot plots combined with box plots e + geom_boxplot(width = 0.5) + geom_dotplot(binaxis = "y", stackdir = "center", fill = "lightgray") # Dot plot + violin plot + stat summary e + geom_violin(trim = FALSE) + geom_dotplot(binaxis='y', stackdir='center', fill = "#999999") + stat_summary(fun.data="mean_sdl", fun.args = list(mult=1))
mean_sdl is used for adding mean and standard deviation. It computes the mean plus or minus a constant times the standard deviation. In the R code above, the constant is specified using the argument
mult (mult = 1). By default mult = 2. The mean +/- SD can be added as a crossbar or a pointrange.
Create a Dot Plot with multiple groups
Two different grouping variables are used:
dose on x-axis and
supp as color (legend variable).
The space between the grouped plots is adjusted using the function
# Change dots fill color by groups e + geom_boxplot(width = 0.5) + geom_dotplot(aes(fill = supp), binaxis='y', stackdir='center')+ scale_fill_manual(values = c("#00AFBB", "#E7B800")) # Change the position : interval between dot plot of the same group e + geom_boxplot(aes(color = supp), width = 0.5, position = position_dodge(0.8)) + geom_dotplot(aes(fill = supp, color = supp), binaxis='y', stackdir='center', dotsize = 0.8,position = position_dodge(0.8))+ scale_fill_manual(values = c("#00AFBB", "#E7B800"))+ scale_color_manual(values = c("#00AFBB", "#E7B800"))
This article describes how to create a Dot Plot using the ggplot2 package.
Recommended for you
This section contains best data science and self-development resources to help you on your path.
Coursera - Online Courses and Specialization
- Course: Machine Learning: Master the Fundamentals by Stanford
- Specialization: Data Science by Johns Hopkins University
- Specialization: Python for Everybody by University of Michigan
- Courses: Build Skills for a Top Job in any Industry by Coursera
- Specialization: Master Machine Learning Fundamentals by University of Washington
- Specialization: Statistics with R by Duke University
- Specialization: Software Development in R by Johns Hopkins University
- Specialization: Genomic Data Science by Johns Hopkins University
Popular Courses Launched in 2020
- Google IT Automation with Python by Google
- AI for Medicine by deeplearning.ai
- Epidemiology in Public Health Practice by Johns Hopkins University
- AWS Fundamentals by Amazon Web Services
- The Science of Well-Being by Yale University
- Google IT Support Professional by Google
- Python for Everybody by University of Michigan
- IBM Data Science Professional Certificate by IBM
- Business Foundations by University of Pennsylvania
- Introduction to Psychology by Yale University
- Excel Skills for Business by Macquarie University
- Psychological First Aid by Johns Hopkins University
- Graphic Design by Cal Arts
Amazing Selling Machine
- Free Training - How to Build a 7-Figure Amazon FBA Business You Can Run 100% From Home and Build Your Dream Life! by ASM
Books - Data Science
- Practical Guide to Cluster Analysis in R by A. Kassambara (Datanovia)
- Practical Guide To Principal Component Methods in R by A. Kassambara (Datanovia)
- Machine Learning Essentials: Practical Guide in R by A. Kassambara (Datanovia)
- R Graphics Essentials for Great Data Visualization by A. Kassambara (Datanovia)
- GGPlot2 Essentials for Great Data Visualization in R by A. Kassambara (Datanovia)
- Network Analysis and Visualization in R by A. Kassambara (Datanovia)
- Practical Statistics in R for Comparing Groups: Numerical Variables by A. Kassambara (Datanovia)
- Inter-Rater Reliability Essentials: Practical Guide in R by A. Kassambara (Datanovia)
- R for Data Science: Import, Tidy, Transform, Visualize, and Model Data by Hadley Wickham & Garrett Grolemund
- Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems by Aurelien Géron
- Practical Statistics for Data Scientists: 50 Essential Concepts by Peter Bruce & Andrew Bruce
- Hands-On Programming with R: Write Your Own Functions And Simulations by Garrett Grolemund & Hadley Wickham
- An Introduction to Statistical Learning: with Applications in R by Gareth James et al.
- Deep Learning with R by François Chollet & J.J. Allaire
- Deep Learning with Python by François Chollet
Give a comment