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)

References

Top Articles
Homemade Baked Tortilla Chips Recipe - Evolving Table
The Best Stuffing Recipe (Classic Thanksgiving Dish)
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
Ascension St. Vincent's Lung Institute - Riverside
Understanding British Money: What's a Quid? A Shilling?
Xenia Canary Dragon Age Origins
Momokun Leaked Controversy - Champion Magazine - Online Magazine
Maine Coon Craigslist
‘An affront to the memories of British sailors’: the lies that sank Hollywood’s sub thriller U-571
Tyreek Hill admits some regrets but calls for officer who restrained him to be fired | CNN
Haverhill, MA Obituaries | Driscoll Funeral Home and Cremation Service
Rogers Breece Obituaries
Ems Isd Skyward Family Access
Elektrische Arbeit W (Kilowattstunden kWh Strompreis Berechnen Berechnung)
Omni Id Portal Waconia
Kellifans.com
Banned in NYC: Airbnb One Year Later
Four-Legged Friday: Meet Tuscaloosa's Adoptable All-Stars Cub & Pickle
Model Center Jasmin
Ice Dodo Unblocked 76
Is Slatt Offensive
Labcorp Locations Near Me
Storm Prediction Center Convective Outlook
Experience the Convenience of Po Box 790010 St Louis Mo
Fungal Symbiote Terraria
modelo julia - PLAYBOARD
Abby's Caribbean Cafe
Joanna Gaines Reveals Who Bought the 'Fixer Upper' Lake House and Her Favorite Features of the Milestone Project
Tri-State Dog Racing Results
Navy Qrs Supervisor Answers
Trade Chart Dave Richard
Lincoln Financial Field Section 110
Free Stuff Craigslist Roanoke Va
Stellaris Resolution
Wi Dept Of Regulation & Licensing
Pick N Pull Near Me [Locator Map + Guide + FAQ]
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
Hooda Math—Games, Features, and Benefits — Mashup Math
Dermpathdiagnostics Com Pay Invoice
How To Use Price Chopper Points At Quiktrip
Maria Butina Bikini
Busted Newspaper Zapata Tx
Latest Posts
Article information

Author: Arielle Torp

Last Updated:

Views: 6457

Rating: 4 / 5 (61 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Arielle Torp

Birthday: 1997-09-20

Address: 87313 Erdman Vista, North Dustinborough, WA 37563

Phone: +97216742823598

Job: Central Technology Officer

Hobby: Taekwondo, Macrame, Foreign language learning, Kite flying, Cooking, Skiing, Computer programming

Introduction: My name is Arielle Torp, I am a comfortable, kind, zealous, lovely, jolly, colorful, adventurous person who loves writing and wants to share my knowledge and understanding with you.