ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (2024)

  • Prepare the data
  • Basic stripcharts
  • Add summary statistics on a stripchart
    • Add mean and median points
    • Stripchart with box blot and violin plot
    • Add mean and standard deviation
  • Change point shapes by groups
  • Change stripchart colors by groups
  • Change the legend position
  • Change the order of items in the legend
  • Stripchart with multiple groups
  • Customized stripcharts
  • Infos

This R tutorial describes how to create a stripchart using R software and ggplot2 package. Stripcharts are also known as one dimensional scatter plots. These plots are suitable compared to box plots when sample sizes are small.

The function geom_jitter() is used.

ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (1)

ToothGrowth data sets are used :

# Convert the variable dose from a numeric to a factor variableToothGrowth$dose <- as.factor(ToothGrowth$dose)head(ToothGrowth)
## 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## 5 6.4 VC 0.5## 6 10.0 VC 0.5

Make sure that the variable dose is converted as a factor variable using the above R script.

library(ggplot2)# Basic stripchartggplot(ToothGrowth, aes(x=dose, y=len)) + geom_jitter()# Change the position# 0.2 : degree of jitter in x directionp<-ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_jitter(position=position_jitter(0.2))p# Rotate the stripchartp + coord_flip()

ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (2)ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (3)ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (4)

Choose which items to display :

p + scale_x_discrete(limits=c("0.5", "2"))

ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (5)

Change point shapes and size :

# Change point sizeggplot(ToothGrowth, aes(x=dose, y=len)) + geom_jitter(position=position_jitter(0.2), cex=1.2)# Change shapeggplot(ToothGrowth, aes(x=dose, y=len)) + geom_jitter(position=position_jitter(0.2), shape=17)

ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (6)ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (7)

Read more on point shapes : ggplot2 point shapes

The function stat_summary() can be used to add mean/median points and more to a stripchart.

Add mean and median points

# stripchart with mean pointsp + stat_summary(fun.y=mean, geom="point", shape=18, size=3, color="red")# stripchart with median pointsp + stat_summary(fun.y=median, geom="point", shape=18, size=3, color="red")

ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (8)ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (9)

Stripchart with box blot and violin plot

# Add basic box plotggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot()+ geom_jitter(position=position_jitter(0.2))# Add notched box plotggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot(notch = TRUE)+ geom_jitter(position=position_jitter(0.2))# Add violin plotggplot(ToothGrowth, aes(x=dose, y=len)) + geom_violin(trim = FALSE)+ geom_jitter(position=position_jitter(0.2))

ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (10)ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (11)ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (12)

Read more on box plot : ggplot2 box plot

Read more on violin plot : ggplot2 violin plot

Add mean and standard deviation

The function mean_sdl is used. mean_sdl computes the mean plus or minus a constant times the standard deviation.

In the R code below, 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 :

p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_jitter(position=position_jitter(0.2))p + stat_summary(fun.data="mean_sdl", mult=1, geom="crossbar", width=0.5)p + stat_summary(fun.data=mean_sdl, mult=1, geom="pointrange", color="red")

ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (13)ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (14)

Note that, you can also define a custom function to produce summary statistics as follow

# Function to produce summary statistics (mean and +/- sd)data_summary <- function(x) { m <- mean(x) ymin <- m-sd(x) ymax <- m+sd(x) return(c(y=m,ymin=ymin,ymax=ymax))}

Use a custom summary function :

p + stat_summary(fun.data=data_summary, color="blue")

ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (15)

In the R code below, point shapes are controlled automatically by the variable dose.

You can also set point shapes manually using the function scale_shape_manual()

# Change point shapes by groupsp<-ggplot(ToothGrowth, aes(x=dose, y=len, shape=dose)) + geom_jitter(position=position_jitter(0.2))p# Change point shapes manuallyp + scale_shape_manual(values=c(1,17,19))

ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (16)ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (17)

Read more on point shapes : ggplot2 point shapes

In the R code below, point colors of the stripchart are automatically controlled by the levels of dose :

# Use single colorggplot(ToothGrowth, aes(x=dose, y=len)) + geom_jitter(position=position_jitter(0.2), color="red")# Change stripchart colors by groupsp<-ggplot(ToothGrowth, aes(x=dose, y=len, color=dose)) + geom_jitter(position=position_jitter(0.2))p

ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (18)ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (19)

It is also possible to change manually stripchart colors using the functions :

  • scale_color_manual() : to use custom colors
  • scale_color_brewer() : to use color palettes from RColorBrewer package
  • scale_color_grey() : to use grey color palettes
# Use custom color palettesp+scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))# Use brewer color palettesp+scale_color_brewer(palette="Dark2")# Use grey scalep + scale_color_grey() + theme_classic()

ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (20)ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (21)ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (22)

Read more on ggplot2 colors here : ggplot2 colors

p + theme(legend.position="top")p + theme(legend.position="bottom")p + theme(legend.position="none")# Remove legend

ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (23)ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (24)ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (25)

The allowed values for the arguments legend.position are : “left”,“top”, “right”, “bottom”.

Read more on ggplot legends : ggplot2 legend

The function scale_x_discrete can be used to change the order of items to “2”, “0.5”, “1” :

p + scale_x_discrete(limits=c("2", "0.5", "1"))

ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (26)

# Change stripchart colors by groupsggplot(ToothGrowth, aes(x=dose, y=len, color=supp)) + geom_jitter(position=position_jitter(0.2))# Change the position : interval between stripchart of the same groupp<-ggplot(ToothGrowth, aes(x=dose, y=len, color=supp, shape=supp)) + geom_jitter(position=position_dodge(0.8))p

ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (27)ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (28)

Change stripchart colors and add box plots :

# Change colorsp+scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))# Add box plotsggplot(ToothGrowth, aes(x=dose, y=len, color=supp)) + geom_boxplot(color="black")+ geom_jitter(position=position_jitter(0.2))# Change the positionggplot(ToothGrowth, aes(x=dose, y=len, color=supp)) + geom_boxplot(position=position_dodge(0.8))+ geom_jitter(position=position_dodge(0.8))

ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (29)ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (30)ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (31)

# Basic stripchartggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot()+ geom_jitter(position=position_jitter(0.2))+ labs(title="Plot of length by dose",x="Dose (mg)", y = "Length")+ theme_classic()# Change color/shape by groupsp <- ggplot(ToothGrowth, aes(x=dose, y=len, color=dose, shape=dose)) + geom_jitter(position=position_jitter(0.2))+ labs(title="Plot of length by dose",x="Dose (mg)", y = "Length")p + theme_classic()

ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (32)ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (33)

Change colors manually :

# Continuous colorsp + scale_color_brewer(palette="Blues") + theme_classic()# Discrete colorsp + scale_color_brewer(palette="Dark2") + theme_minimal()# Gradient colorsp + scale_color_brewer(palette="RdBu")

ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (34)ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (35)ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (36)

Read more on ggplot2 colors here : ggplot2 colors

This analysis has been performed using R software (ver. 3.1.2) and ggplot2 (ver. 1.0.0)

ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization - Easy Guides - Wiki (2024)

FAQs

What does jitter do in ggplot2? ›

The jitter geom is a convenient shortcut for geom_point(position = "jitter") . It adds a small amount of random variation to the location of each point, and is a useful way of handling overplotting caused by discreteness in smaller datasets.

How to plot data in R using ggplot2? ›

7 Plotting with ggplot2
  1. Start by preparing a dataset so that it is in the right format.
  2. Create a plot object using the function ggplot() .
  3. Define so-called “aesthetic mappings”, i.e. we determine which variables should be displayed on the X and Y axes and which variables are used to group the data.

What are the three key components required to create a useful Ggplot using the ggplot2 package? ›

About the mpg dataset included with ggplot2, Section 2.2. The three key components of every plot: data, aesthetics and geoms, Section 2.3.

Why ggplot2 is the best? ›

It allows to highlight the main message of the chart, turning a messy figure in an insightful medium. ggplot2 offers many function for this purpose, allowing to add all sorts of text and shapes. geom_text() allows to add annotation to one, several or all markers of your chart.

What can jitter do? ›

The effects of jitter

Flickering display monitors, delayed data transmission when video streaming and poor processor performance are all results of internet jitter.

Why do we use jitter? ›

The jitter function is used to add random noise to a numeric vector, which can be helpful when visualizing data in a scatterplot. By using the jitter function, we can get a better picture of the true underlying relationship between two variables in a dataset.

What is ggplot2 used for? ›

ggplot2 is an R package for producing statistical, or data, graphics. Unlike most other graphics packages, ggplot2 has an underlying grammar, based on the Grammar of Graphics (Wilkinson 2005), that allows you to compose graphs by combining independent components. This makes ggplot2 powerful.

What is the difference between ggplot and ggplot2? ›

ggplot2 is the latest version of the popular open-source data visualization tool ggplot for R, used to create plots using the function ggplot(). It is part of the R tidyverse ecosystem, designed with common APIs, and is used in the statistical programming language.

How do I get ggplot2 to work in R? ›

The ggplot2 package can be easily installed using the R function install. packages() . The above code will automatically download the ggplot2 package, from the CRAN (Comprehensive R Archive Network) repository, and install it.

How many elements are in ggplot2? ›

Although each ggplot2 plot contains all of the eight elements, you've seen that you won't always need to specify each of the eight. The following plot only specifies three (data, geom, aesthetic mappings).

How many shapes are there in ggplot? ›

Geoms that draw points have a "shape" parameter. Legal shape values are the numbers 0 to 25, and the numbers 32 to 127. Only shapes 21 to 25 are filled (and thus are affected by the fill color), the rest are just drawn in the outline color.

How many line types are there in ggplot? ›

The scale_linetype_discrete scale maps up to 12 distinct values to 12 pre-defined linetypes.

Why is it called ggplot2? ›

ggplot2 is called ggplot2 because once upon a time there was just a library ggplot. However, the developer noticed that it used an inefficient set of functions.

What are the disadvantages of ggplot2? ›

So why do we need another plotting method, to make the same plot?
ggplot2 Pros:ggplot2 Cons:
consistent, concise syntaxdifferent syntax from the rest of R
intuitive (to many)does not handle a few types of output well
visually appealing by default
entirely customizable

What are the benefits of using ggplot2? ›

2 The advantages of ggplot2

Its consistent and logical syntax makes it easy to learn and use, while its high-quality graphics can be customized and refined with various options and themes. Furthermore, it supports a wide range of plot types, from simple histograms and boxplots to complex maps and animations.

What does jitter do in R? ›

jitter(x, ...) returns a numeric of the same length as x , but with an amount of noise added in order to break ties.

What does a jitter plot show? ›

A jitter plot represents data points in the form of single dots, in a similar manner to a scatter plot. The difference is that the jitter plot helps visualize the relationship between a measurement variable and a categorical variable.

What is the jitter in a scatter plot? ›

Scatter plots with jittering are used when individual points are so dense that visual inspection suffers. This chart displays experiment results with a random horizontal jittering to keep them apart. The chart has 1 X axis displaying categories. The chart has 1 Y axis displaying Measurements.

What is jitter in strip plot? ›

A jitter plot is a variant of the strip plot with a better view of overlapping data points, used to visualise the distribution of many individual one-dimensional values.

Top Articles
Banana Republic Vs Banana Republic Factory: Differences • Ready Sleek
Save 80% on Michael Kors, 50% on Banana Republic, 70% on Gap & More
Spasa Parish
Rentals for rent in Maastricht
159R Bus Schedule Pdf
Sallisaw Bin Store
Black Adam Showtimes Near Maya Cinemas Delano
Espn Transfer Portal Basketball
Pollen Levels Richmond
11 Best Sites Like The Chive For Funny Pictures and Memes
Things to do in Wichita Falls on weekends 12-15 September
Craigslist Pets Huntsville Alabama
Paulette Goddard | American Actress, Modern Times, Charlie Chaplin
Red Dead Redemption 2 Legendary Fish Locations Guide (“A Fisher of Fish”)
What's the Difference Between Halal and Haram Meat & Food?
R/Skinwalker
Rugged Gentleman Barber Shop Martinsburg Wv
Jennifer Lenzini Leaving Ktiv
Justified - Streams, Episodenguide und News zur Serie
Epay. Medstarhealth.org
Olde Kegg Bar & Grill Portage Menu
Cubilabras
Half Inning In Which The Home Team Bats Crossword
Amazing Lash Bay Colony
Juego Friv Poki
Dirt Devil Ud70181 Parts Diagram
Truist Bank Open Saturday
Water Leaks in Your Car When It Rains? Common Causes & Fixes
What’s Closing at Disney World? A Complete Guide
New from Simply So Good - Cherry Apricot Slab Pie
Drys Pharmacy
Ohio State Football Wiki
Find Words Containing Specific Letters | WordFinder®
FirstLight Power to Acquire Leading Canadian Renewable Operator and Developer Hydromega Services Inc. - FirstLight
Webmail.unt.edu
2024-25 ITH Season Preview: USC Trojans
Metro By T Mobile Sign In
Restored Republic December 1 2022
12 30 Pacific Time
Jami Lafay Gofundme
Greenbrier Bunker Tour Coupon
No Compromise in Maneuverability and Effectiveness
Crystal Westbrooks Nipple
Ice Hockey Dboard
Über 60 Prozent Rabatt auf E-Bikes: Aldi reduziert sämtliche Pedelecs stark im Preis - nur noch für kurze Zeit
Wie blocke ich einen Bot aus Boardman/USA - sellerforum.de
Infinity Pool Showtimes Near Maya Cinemas Bakersfield
Dermpathdiagnostics Com Pay Invoice
How To Use Price Chopper Points At Quiktrip
Maria Butina Bikini
Busted Newspaper Zapata Tx
Latest Posts
Article information

Author: Van Hayes

Last Updated:

Views: 5688

Rating: 4.6 / 5 (46 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Van Hayes

Birthday: 1994-06-07

Address: 2004 Kling Rapid, New Destiny, MT 64658-2367

Phone: +512425013758

Job: National Farming Director

Hobby: Reading, Polo, Genealogy, amateur radio, Scouting, Stand-up comedy, Cryptography

Introduction: My name is Van Hayes, I am a thankful, friendly, smiling, calm, powerful, fine, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.