# Data Visualization using GGPlot2

## GGPlot Stripchart 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:

#### Related Book

GGPlot2 Essentials for Great Data Visualization in R

## Key R functions

• Key function: geom_jitter()
• key arguments: color, fill, size, shape. Changes points color, fill, size and shape

## 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

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)
# Initiate a ggplot
e <- ggplot(ToothGrowth, aes(x = dose, y = len))

# Stripcharts with summary statistics
# Change color by dose groups
e + 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")) 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 plot
e + geom_boxplot() +
geom_jitter(position = position_jitter(0.2))

# Strip chart + violin plot + stat summary
e + geom_violin(trim = FALSE) +
geom_jitter(position = position_jitter(0.2)) +
stat_summary(fun.data="mean_sdl",  fun.args = list(mult=1),
color = "red") ## 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")) ## Conclusion

This article describes how to create a stripchart using the ggplot2 package.
{"ft_lang_label":"__label__en","ft_lang_prob":0.61035013,"math_prob":0.9899382,"size":3112,"snap":"2019-51-2020-05","text_gpt3_token_len":856,"char_repetition_ratio":0.117117114,"word_repetition_ratio":0.008474576,"special_character_ratio":0.2779563,"punctuation_ratio":0.1631579,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99435186,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12],"im_url_duplicate_count":[null,1,null,5,null,5,null,5,null,5,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-12-07T18:36:16Z\",\"WARC-Record-ID\":\"<urn:uuid:36b7e92e-80ac-4dfa-9ba9-b6175f094a10>\",\"Content-Length\":\"76603\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e5d03151-26bf-4c1c-8678-39c2ad95c7bd>\",\"WARC-Concurrent-To\":\"<urn:uuid:bb667c8b-69c3-4086-8fad-3e8441f551ba>\",\"WARC-IP-Address\":\"188.130.25.202\",\"WARC-Target-URI\":\"https://www.datanovia.com/en/lessons/ggplot-stripchart/\",\"WARC-Payload-Digest\":\"sha1:DHWIKCXX2AEMRED36HZRWIW4KM3VQQJX\",\"WARC-Block-Digest\":\"sha1:I3VPWVBEO5PAOSFYSD5VRQEO24FVPXM7\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-51/CC-MAIN-2019-51_segments_1575540501887.27_warc_CC-MAIN-20191207183439-20191207211439-00167.warc.gz\"}"}