GGPlot Stripchart Best Reference - Datanovia (2024)

GGPlot Stripchart

10 mins

Data Visualization using GGPlot2

282222312524301216

Stripcharts are also known as one dimensional scatter plots. These plots are suitable compared to box plots when sample sizes are small.

This article describes how to create and customize Stripcharts using the ggplot2 R package.



Contents:

  • Key R functions
  • Data preparation
  • Loading required R package
  • Basic stripcharts
  • Combine with box plots and violin plots
  • Create a stripchart with multiple groups
  • Conclusion

Related Book

GGPlot2 Essentials for Great Data Visualization in R

Data preparation

  • Demo dataset: ToothGrowth
    • 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.

First, convert the variable dose from a numeric to a discrete factor variable:

data("ToothGrowth")ToothGrowth$dose <- as.factor(ToothGrowth$dose)head(ToothGrowth, 3)
## len supp dose## 1 4.2 VC 0.5## 2 11.5 VC 0.5## 3 7.3 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 stripcharts

We start by initiating a plot named e, then we’ll add layers. The following R code creates stripcharts combined with summary statistics (mean +/- SD), boxplots and violin plots.

  • Change points shape and color by groups
  • Adjust the degree of jittering: position_jitter(0.2)
  • Add summary statistics:
# Initiate a ggplote <- ggplot(ToothGrowth, aes(x = dose, y = len))# Stripcharts with summary statistics# Change color by dose groupse + geom_jitter(aes(shape = dose, color = dose), position = position_jitter(0.2), size = 1.2) + stat_summary(aes(color = dose), size = 0.4, fun.data="mean_sdl", fun.args = list(mult=1))+ scale_color_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))

GGPlot Stripchart Best Reference - Datanovia (1)

The function 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.

Combine with box plots and violin plots

# Combine with box plote + geom_boxplot() + geom_jitter(position = position_jitter(0.2)) # Strip chart + violin plot + stat summarye + geom_violin(trim = FALSE) + geom_jitter(position = position_jitter(0.2)) + stat_summary(fun.data="mean_sdl", fun.args = list(mult=1), color = "red")

GGPlot Stripchart Best Reference - Datanovia (2)GGPlot Stripchart Best Reference - Datanovia (3)

Create a stripchart with multiple groups

The R code is similar to what we have seen in dot plots section. However, to create dodged jitter points, you should use the function position_jitterdodge() instead of position_dodge().

e + geom_jitter( aes(shape = supp, color = supp), size = 1.2, position = position_jitterdodge(jitter.width = 0.2, dodge.width = 0.8) ) + stat_summary( aes(color = supp), fun.data="mean_sdl", fun.args = list(mult=1), size = 0.4, position = position_dodge(0.8) )+ scale_color_manual(values = c("#00AFBB", "#E7B800"))

GGPlot Stripchart Best Reference - Datanovia (4)

Conclusion

This article describes how to create a stripchart 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

Data science

  • 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

Trending Courses

  • 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

Amazon FBA

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

Our Books

  • 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)

Others

  • 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

Version: Français

GGPlot Dot Plot (Prev Lesson)

(Next Lesson) GGPlot Line Plot

Back to Data Visualization using GGPlot2

No Comments

Give a comment

Course Curriculum

  • Introduction to GGPlot2

    20 mins

  • GGPlot Scatter Plot

    15 mins

  • GGPlot Boxplot

    15 mins

  • GGPlot Violin Plot

    10 mins

  • GGPlot Dot Plot

    15 mins

  • GGPlot Stripchart

    10 mins

  • GGPlot Line Plot

    15 mins

  • GGPlot Barplot

    10 mins

  • GGPlot Error Bars

    15 mins

  • GGPlot Density Plot

    10 mins

  • GGPlot Histogram

    10 mins

  • GGPLOT QQ Plot

    10 mins

  • GGPlot ECDF

    10 mins

  • Combine Multiple GGPlots into a Figure

    15 mins

Teacher

GGPlot Stripchart Best Reference - Datanovia (6)

Alboukadel Kassambara
Role : Founder of Datanovia
  • Website : https://www.datanovia.com/en
  • Experience : >10 years
  • Specialist in : Bioinformatics and Cancer Biology

Read More

GGPlot Stripchart Best Reference - Datanovia (2024)

FAQs

What is the concept of Ggplot? ›

The gg in ggplot2 means Grammar of Graphics, a graphic concept which describes plots by using a “grammar”. According to the ggplot2 concept, a plot can be divided into different fundamental parts: Plot = data + Aesthetics + Geometry. data: a data frame. aesthetics: used to indicate the x and y variables.

What are the two variables in a ggplot scatterplot? ›

A Scatter plot (also known as X-Y plot or Point graph) is used to display the relationship between two continuous variables x and y.

What types of plots can be made in R using Ggplot? ›

In this lesson, you will focus on using ggplot2 to create four types of graphs: a histogram, a scatter plot, a bar plot, and a box plot. However, you can create numerous other types of graphs in ggplot2, such as density plots, box plots, violin plots, maps, and much more.

What is the default shape of Ggplot? ›

By default, shape = 19 (a filled circle). You can change the number to plot different shapes, i.e. geom_point(shape = x) .

How do you change the transparency of points in Ggplot? ›

We can control the transparency of the points with the alpha argument to geom_point . Values of alpha range from 0 to 1, with lower values corresponding to more transparent colors (an alpha of 1 is the default value). Specifically, an alpha of 0.1, would make a point one-tenth as opaque as a normal point.

Can a scatter plot have two sets of data? ›

In Excel, creating a scatter plot with multiple data series can be done several ways. The easiest is to have a single column in your data containing the X values for all of your data series, and then have a separate column for the Y values of each individual data series.

What is an alternative to a scatter plot? ›

Heatmap. As noted above, a heatmap can be a good alternative to the scatter plot when there are a lot of data points that need to be plotted and their density causes overplotting issues.

What is the best plot for continuous variables? ›

Box Plots are the best way to display the distribution of a single continuous variable that needs to be plotted against a categorical variable.

What can I use instead of boxplot in ggplot2? ›

violin plots are similar to box plots, except that they also show the kernel probability density of the data at different values. Typically, violin plots will include a marker for the median of the data and a box indicating the interquartile range, as in standard box plots.

What is the difference between ggplot and ggplot2? ›

You may notice that we sometimes reference 'ggplot2' and sometimes 'ggplot'. To clarify, 'ggplot2' is the name of the most recent version of the package. However, any time we call the function itself, it's just called 'ggplot'.

What can I use instead of ggplot? ›

Alternatives to ggplot2
  • Matplotlib. Matplotlib. Free. ...
  • Qt. Qt. $302 per month. ...
  • Vega-Altair. Vega-Altair. Free. ...
  • Seaborn. Seaborn. Seaborn is a Python data visualization tool that uses matplotlib. ...
  • Semantic UI React. Vercel. Free. ...
  • cryptography. cryptography. Free. ...
  • WebDataRocks. WebDataRocks. Free. ...
  • List & Label. combit. €650/license.

Which of the following functions allows you to change the shape of points in Ggplot? ›

The functions below can be used : scale_shape_manual() : to change point shapes. scale_color_manual() : to change point colors. scale_size_manual() : to change the size of points.

How do I change the dot size in Ggplot? ›

The function plotPCA() is a function that has been defined to create the PCA plot using ggplot2 . To change the size of the dots in the PCA plot, you can modify the value of the size argument in the geom_point() function. A size argument set to 1 means the dots will be smaller than when it's set for larger values.

How do I change the order of points in Ggplot? ›

To do this, you can use forcats::fct_infreq() . If you'd like to plot the highest value first, you can also reverse the order with forcats::fct_rev() .

How to change point colors in ggplot? ›

Change the point color/shape/size manually
  1. scale_shape_manual() for point shapes.
  2. scale_color_manual() for point colors.
  3. scale_size_manual() for point sizes.

Top Articles
Latest Posts
Article information

Author: Aron Pacocha

Last Updated:

Views: 5321

Rating: 4.8 / 5 (48 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Aron Pacocha

Birthday: 1999-08-12

Address: 3808 Moen Corner, Gorczanyport, FL 67364-2074

Phone: +393457723392

Job: Retail Consultant

Hobby: Jewelry making, Cooking, Gaming, Reading, Juggling, Cabaret, Origami

Introduction: My name is Aron Pacocha, I am a happy, tasty, innocent, proud, talented, courageous, magnificent person who loves writing and wants to share my knowledge and understanding with you.