library(duke)
library(palmerpenguins)
library(ggmosaic)
library(ggplot2)
library(dplyr)
4 Package Use
This vignette aims to comprehensively demonstrate the use and functionality of the package duke. duke is fully integrated with the ggplot2 and allows for the creation of Duke official branded visualizations that are color blind friendly.
For this purposes of this vignette, we will use the palmerspenguins package, which provides a simple dataset on Antarctic penguins and their characteristics: penguins
(Horst, Hill, and Gorman, n.d.). The dataset has eight variables - some numeric and some categorical - and 344 observations, each representing a unique penguin.
4.1 Scatter Plot - Continuous Color
<- ggplot(
scatterplot_c
penguins,aes(x = bill_length_mm, y = bill_depth_mm)
+
) geom_point(aes(color = body_mass_g)) +
labs(
title = "Bill Length vs. Bill Depth",
x = "Bill Length (mm)", y = "Bill Depth (mm)"
)
+
scatterplot_c scale_duke_continuous() +
theme_duke()
+
scatterplot_c geom_point(aes(shape = species)) +
scale_duke_continuous() +
theme_duke()
4.2 Scatter Plot - Discrete Color
<- ggplot(
scatterplot_d
penguins,aes(x = bill_length_mm, y = bill_depth_mm, color = species)
+
) geom_point(size = 3) +
labs(
title = "Bill Length vs. Bill Depth",
caption = "(Colors used) \n Duke Royal Blue, Eno, Copper",
x = "Bill Length (mm)",
y = "Bill Depth (mm)"
)
+
scatterplot_d theme_duke() +
scale_duke_color_discrete()
+
scatterplot_d facet_wrap(~species) +
theme_duke() +
scale_duke_color_discrete()
4.3 Bar Plot
<-
barplot ggplot(penguins, aes(x = species, fill = species)) +
geom_bar() +
labs(
title = "Distribution of Penguin Species",
caption = "(Colors from left to right) \n Duke Royal Blue, Duke Navy Blue, Copper",
x = "Species",
y = "Count"
)
+
barplot scale_duke_fill_discrete() +
theme_duke()
<- penguins %>%
m_penguins ::filter(sex == "male")
dplyr
<- ggplot(m_penguins, aes(x = sex, fill = sex)) +
barplot2 geom_bar()
+
barplot2 scale_duke_fill_discrete() +
theme_duke()
# 8-category plot
<- ggplot(students, aes(x = grade, fill = grade)) +
barplot8 geom_bar()
+
barplot8 scale_duke_fill_discrete() +
theme_duke()
# 7-category plot
<- students %>%
barplot7 slice(-13) %>%
ggplot(aes(x = grade, fill = grade)) +
geom_bar() +
scale_duke_fill_discrete() +
theme_duke()
# 6-category plot
<- students %>%
barplot6 slice(-c(9, 10, 13)) %>%
ggplot(aes(x = grade, fill = grade)) +
geom_bar() +
scale_duke_fill_discrete() +
theme_duke()
# 5-category plot
<- students %>%
barplot5 slice(-c(9, 10, 13, 7, 18)) %>%
ggplot(aes(x = grade, fill = grade)) +
geom_bar() +
scale_duke_fill_discrete() +
theme_duke()
# 4-category plot
<- students %>%
barplot4 slice(-c(9, 10, 13, 7, 18, 4, 8)) %>%
ggplot(aes(x = grade, fill = grade)) +
geom_bar() +
scale_duke_fill_discrete() +
theme_duke()
4.4 Histogram
<- ggplot(penguins, aes(body_mass_g)) +
histplot geom_histogram(aes(fill = species), alpha = 0.8) +
labs(title = "Distribution of Penguin Body Mass",
caption = "(Colors used) \n Duke Royal Blue, Duke Navy Blue, Copper",
x = "Body Mass (g)",
y = "Count")
+
histplot scale_duke_fill_discrete() +
theme_duke()
4.5 Box Plot
<- ggplot(penguins, aes(sex, body_mass_g)) +
boxplot geom_boxplot() +
labs(
title = "Comparison of Body Mass By Sex",
x = "Sex",
y = "Body Mass (g)"
)
+
boxplot theme_duke()
4.6 Density Plot
<- ggplot(penguins, aes(bill_depth_mm)) +
densityplot geom_density(aes(fill = species)) +
labs(
title = "Density of Penguin Bill Depth",
x = "Bill Depth (mm)",
y = "Densiy"
)
+
densityplot scale_duke_fill_discrete() +
theme_duke()
4.7 Jitter Plot - Discrete Color
<- ggplot(penguins, aes(as.factor(year), body_mass_g)) +
jitterplot_d geom_jitter(aes(color = species)) +
labs(
title = "Comparison of Body Mass By Year",
x = "Year",
y = "Body Mass (g)"
)
+
jitterplot_d scale_duke_color_discrete() +
theme_duke()
4.8 Jitter Plot - Continuous Color
<- ggplot(penguins, aes(as.factor(year), body_mass_g)) +
jitterplot_c geom_jitter(aes(color = bill_length_mm)) +
labs(
title = "Comparison of Body Mass By Year",
x = "Year",
y = "Body Mass (g)"
)
+
jitterplot_c scale_duke_continuous() +
theme_duke()
4.9 Mosaic Plot
<- ggplot(data = penguins) +
mosaicplot ::geom_mosaic(aes(x = ggmosaic::product(sex, island), fill = sex)) +
ggmosaiclabs(
title = "Penguin Sex vs. Island",
x = "Island",
y = "Penguin Sex",
caption = "(Color from top to bottom) \n Granite, Duke Navy Blue, Duke Royal Blue"
)
+
mosaicplot scale_duke_fill_discrete() +
theme_duke()
4.10 Label Plot
<- ggplot(
labelplot
penguins,aes(bill_length_mm, bill_depth_mm,
fill = island
)+
) geom_label(aes(label = island)) +
labs(
title = "Bill Length/Depth by Island",
x = "Bill Length (mm)",
y = "Bill Depth (mm)"
)
+
labelplot scale_duke_fill_discrete() +
theme_duke()
4.11 Quantile Plot
<-
quantileplot ggplot(
penguins,aes(bill_length_mm, bill_depth_mm, color = species)
+
) geom_quantile() +
labs(title = "Bill Length/Depth Quantiles",
x = "Bill Length (mm)",
y = "Bill Depth (mm)")
+
quantileplot scale_duke_color_discrete() +
theme_duke()
4.12 Area Plot
<- ggplot(penguins, aes(body_mass_g, fill = species)) +
areaplot geom_area(stat = "bin") +
labs(
title = "Area of Body Mass Index",
x = "Body Mass (g)"
)
+
areaplot scale_duke_fill_discrete() +
theme_duke()
4.13 Dot Plot
<- ggplot(penguins, aes(body_mass_g)) +
dotplot geom_dotplot(aes(fill = species)) +
labs(
title = "Dotplot of Body Mass (g)",
x = "Body Mass (g)"
)
+
dotplot scale_duke_fill_discrete() +
theme_duke()
4.14 Frequency Polynomial Plot
<-
freqplot ggplot(penguins, aes(body_mass_g)) +
geom_freqpoly(aes(color = species)) +
labs(title = "Frequency Polynomial Plot of Body Mass (g)",
x = "Body Mass (g)")
+
freqplot scale_duke_color_discrete() +
theme_duke()
4.15 Column Plot
<- ggplot(penguins, aes(species, body_mass_g, color = species)) +
colplot geom_col() +
labs(
title = "Body Mass (g) By Species",
x = "Species",
y = "Body Mass (g)"
)
+
colplot scale_duke_color_discrete() +
theme_duke()
4.16 Violin Plot
<- ggplot(penguins, aes(species, body_mass_g, fill = species)) +
violinplot geom_violin(scale = "area") +
labs(
title = "Body Mass (g) By Species",
x = "Species",
y = "Body Mass (g)"
)
+
violinplot scale_duke_fill_discrete() +
theme_duke()
4.17 Count Plot
<- ggplot(penguins, aes(species, island, color = species)) +
countplot geom_count() +
labs(
title = "Species Count by Island",
x = "Species",
y = "Island"
)
+
countplot scale_duke_color_discrete() +
theme_duke()
4.18 Step Plot
<- ggplot(
stepplot
penguins, aes(as.factor(year), body_mass_g, color = species)
+
) geom_step() +
labs(
itle = "Body Mass (g) By Year",
x = "Year",
y = "Body Mass (g)"
)
+
stepplot scale_duke_color_discrete() +
theme_duke()