# Install packages
if (!requireNamespace("grafify", quietly = TRUE)) {
install.packages("grafify")
}if (!requireNamespace("dplyr", quietly = TRUE)) {
install.packages("dplyr")
}
# Load packages
library(grafify)
library(dplyr)
Density-Histogram
Use density plots or histograms to show data distribution.
Setup
System Requirements: Cross-platform (Linux/MacOS/Windows)
Programming language: R
Dependent packages:
grafify
;dplyr
Data Preparation
# Load data
<- read.delim("files/Hiplot/039-density-histogram-data.txt", header = T)
data
# convert data structure
<- "Doubling_time"
y <- "Student"
group <- factor(data[, group], levels = unique(data[, group]))
data[, group] <- data %>%
data mutate(median = median(get(y), na.rm = TRUE),
mean = mean(get(y), na.rm = TRUE))
# View data
head(data)
Experiment Student Doubling_time facet median mean
1 Exp1 A 17.36765 F1 20.18114 19.91642
2 Exp1 B 18.04119 F1 20.18114 19.91642
3 Exp1 C 18.70120 F1 20.18114 19.91642
4 Exp1 D 20.06762 F1 20.18114 19.91642
5 Exp1 E 20.19807 F2 20.18114 19.91642
6 Exp1 F 22.11908 F2 20.18114 19.91642
Visualization
1. Density Plot
# Density Plot
<- plot_density(
p data = data,
ycol = get(y),
group = get(group),
linethick = 0.5,
c_alpha = 0.6) +
ggtitle("Density Plot") +
geom_vline(aes_string(xintercept = "median"),
colour = 'black', linetype = 2, size = 0.5) +
xlab(y) +
ylab("density") +
guides(fill = guide_legend(title = group), color = FALSE) +
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 = "bottom",
legend.direction = "horizontal",
legend.title = element_text(size = 10),
legend.text = element_text(size = 10))
p

2. Histogram Plot
# Histogram Plot
<- plot_histogram(
p data = data,
ycol = get(y),
group = get(group),
linethick = 0.5,
BinSize = 30) +
ggtitle("Histogram Plot") +
geom_vline(aes_string(xintercept = "median"),
colour = 'black', linetype = 2, size = 0.5) +
xlab(y) +
ylab("density") +
guides(fill = guide_legend(title = group), color = FALSE) +
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 = "bottom",
legend.direction = "horizontal",
legend.title = element_text(size = 10),
legend.text = element_text(size = 10))
p
