Advanced plot part 1

Training Advance plots in gglpot & extensions packages

R programming Quarto Analysis Report for plot training
Author
Affiliation

Fredrick Onduru

Biostatistician-Analyst

Published

August 14, 2023

Code
library(ggdist)
library(ggbeeswarm)
library(patchwork)
library(PupillometryR)
library(janitor)
library(ggthemes)
library(tidyquant)
library(tidylog)
library(flextable)
library(tidyverse)
Description of iris data set

iris is a data frame with 150 cases (rows) and 5 variables (columns) named Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, and Species.

Code
data("iris")
iris %>% head(5) %>% flextable::flextable()
Table 1:

Iris data set preview

Sepal.Length

Sepal.Width

Petal.Length

Petal.Width

Species

5.1

3.5

1.4

0.2

setosa

4.9

3.0

1.4

0.2

setosa

4.7

3.2

1.3

0.2

setosa

4.6

3.1

1.5

0.2

setosa

5.0

3.6

1.4

0.2

setosa

0.1 Exploring Sepal Length distribution

Key plot for Data distribution

Different ways to communicate the normality, skewness and kurtosis of continuous variables by a grouping/ cluster/ treatment variable.

Usually, before perform any hypothesis test its recommend to Explore the nature of the outcome variable to acertain the above issues revolving around normality.

Code
#   Histogram
theme_set(theme_light())
iris %>% ggplot(aes(x = Sepal.Length,fill = Species, color = Species)) +
  geom_histogram(alpha = 0.5,position = position_stack()) +
  theme(legend.position = c(.90, .85))
  

#  Density plot
theme_set(theme_pander())
iris %>% ggplot(aes(x = Sepal.Length,fill = Species, color = Species)) +
  geom_density(alpha = 0.5) + theme(legend.position = c(.90, .85))

#  Box plot
theme_set(theme_bw())
iris %>% ggplot(aes(x = Species,y = Sepal.Length,fill = Species, color = Species)) +
  geom_boxplot(alpha = 0.5,width = 0.3) +
  theme(legend.position = "none")

#  Violin plot
theme_set(theme_minimal())
iris %>% ggplot(aes(x = Species,y = Sepal.Length,fill = Species, color = Species)) +
  geom_violin(alpha = 0.5,width = 0.5) +
  theme(legend.position = "none")

#  Dot plot
theme_set(theme_minimal())
iris %>% ggplot(aes(x = Species,y = Sepal.Length,fill = Species, color = Species)) +
  geom_dots(alpha = 0.5,dotsize = 0.5) +
  theme(legend.position = "none")


#  Beeswarm plot
theme_set(theme_minimal())
iris %>% ggplot(aes(x = Species,y = Sepal.Length,fill = Species, color = Species)) +
  geom_beeswarm(alpha = 0.5) +
  theme(legend.position = "none")

#  Jitter/strip plot
theme_set(theme_classic())
iris %>% ggplot(aes(x = Species,y = Sepal.Length,fill = Species, color = Species)) +
  geom_jitter(alpha = 0.6,position = position_jitter(width = 0.12)) +
  theme(legend.position = "none")


#  Eye plot
theme_set(theme_light())
iris %>% ggplot(aes(x = Species,y = Sepal.Length,fill = Species, color = Species)) +
  stat_eye(alpha = 0.3) +
  theme(legend.position = "none")

#  Half eye plot
theme_set(theme_pander())
iris %>% ggplot(aes(x = Species,y = Sepal.Length,fill = Species, color = Species)) +
  stat_halfeye(alpha = 0.3, justification = -0.1) +
  theme(legend.position = "none")

#  Rain drop plot
theme_set(theme_bw())
iris %>% ggplot(aes(x = Species,y = Sepal.Length,fill = Species, color = Species)) +
  stat_halfeye(alpha = 0.3, justification = -0.1,adjust = 0.4) +
  geom_boxplot(width = 0.1, alpha = 0.4) +
  stat_dots(side = "left", justification = 1.1,dotsize = 0.5,alpha = 0.5,adjust = 0.4) +
  coord_flip() +
  theme(legend.position = "none")

# Flat violin
theme_set(theme_minimal())
iris %>% ggplot(aes(x = Species,y = Sepal.Length,fill = Species, color = Species)) +
  geom_flat_violin(alpha = 0.5,adjust = 0.5) + 
  stat_dots(side = "left", justification = 1.1,dotsize = 0.5,alpha = 0.5,adjust = 0.4) +
  coord_flip() +
  theme(legend.position = "none")

# Fill Density plot
theme_set(theme_pander())
iris %>% ggplot(aes(x = Sepal.Length,fill = Species, color = Species)) +
  geom_density(alpha = 0.5,position = "fill") + theme(legend.position = c(.90, .85))

(a) Stack Histogram plot

(b) Density plot

(c) Box plot

(d) Violin plot

(e) Dot plot

(f) Beeswarm plot

(g) Jitter/strip plot

(h) Eye plot

(i) Half eye plot”

(j) Rain drop plot

(k) Flat violin

(l) Fill density plot

Figure 1: Distribution of Sepal length by different species of plants