# Install packages
if (!requireNamespace("ggpubr", quietly = TRUE)) {
install.packages("ggpubr")
}
# Load packages
library(ggpubr)
Deviation Plot
Note
Hiplot website
This page is the tutorial for source code version of the Hiplot Deviation Plot
plugin. You can also use the Hiplot website to achieve no code ploting. For more information please see the following link:
Deviation plot provides a visual representation of the differences between data points.
Setup
System Requirements: Cross-platform (Linux/MacOS/Windows)
Programming language: R
Dependent packages:
ggpubr
Data Preparation
# Load data
<- read.delim("files/Hiplot/041-deviation-plot-data.txt", header = T)
data
# convert data structure
"z_score"]] <- (data[["mpg"]] - mean(data[["mpg"]])) / sd(data[["mpg"]])
data[["Group"]] <- factor(ifelse(data[["z_score"]] < 0, "low", "high"),
data[[levels = c("low", "high")
)
# View data
head(data)
mpg cyl disp hp drat wt qsec vs am gear carb name
1 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4
2 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 Mazda RX4 Wag
3 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 Datsun 710
4 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 Hornet 4 Drive
5 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 Hornet Sportabout
6 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 Valiant
z_score Group
1 0.1508848 high
2 0.1508848 high
3 0.4495434 high
4 0.2172534 high
5 -0.2307345 low
6 -0.3302874 low
Visualization
# Deviation Plot
<- ggbarplot(data,
p x = "name",
y = "z_score",
fill = "Group",
color = "white",
sort.val = "desc",
sort.by.groups = FALSE,
x.text.angle = 90,
xlab = "name",
ylab = "mpg",
rotate = TRUE
+
) scale_fill_manual(values = c("#e04d39","#5bbad6")) +
theme_bw() +
theme(text = element_text(family = "Arial"),
plot.title = element_text(size = 12,hjust = 0.5),
axis.title = element_text(size = 12),
axis.text = element_text(size = 10),
axis.text.x = element_text(angle = 0, hjust = 0.5,vjust = 1),
legend.position = "right",
legend.direction = "vertical",
legend.title = element_text(size = 10),
legend.text = element_text(size = 10))
p
