R 数据可视化:Z-Score 得分图
2021-11-10 本文已影响0人
watermark
简介
z-score得分图简介.png数据集中的变量通常具有不同的量纲和数量级,当不同变量的取值相差较大时,如果直接使用原始值进行分析,就会突出取值较大的变量的作用,削弱取值较小的变量的作用。为消除量纲与数量级对数据分析的影响,需要对数据进行标准化处理。z-score 就是一种常用的数据标准化方法。z-score 得分图就是将所有标准化值以散点的方式标注在图中,这让具有不同量级、量纲的变量的纵向比较成为可能,用以预览所有变量的标准化取值分布情况,对于分类数据而言,还能纵览变量对不通类别样本的区分能力。
Z-Score 计算公式
变量 = {, , … , } 经 Z-Score 处理后的变量为 = { , , … , },其中:
需要什么格式的数据
数据格式为 样本×变量,即行为样本,列为变量。group 列代表样本分类 label。
数据格式.png开始作图
1. 数据准备
data = data.frame(group = c("control", "control", "control", "control", "control", "control",
"test", "test", "test", "test", "test", "test"),
L.Carnitine = c(2.34, 1.71, 2.62, 2.77, 1.87, 2.30,
2.75, 2.73, 1.99, 2.76, 3.06, 2.35),
Acetylcarnitine = c(14.69, 14.27, 14.82, 14.80, 14.87, 14.83,
14.61, 14.37, 13.86, 14.40, 14.26, 13.98),
Creatine = c(29.49, 29.54, 29.58, 29.60, 29.68, 29.87,
29.68, 29.60, 29.39, 29.69, 29.69, 29.44),
Choline = c(23.18, 23.36, 22.91, 23.53, 22.81, 23.42,
23.04, 23.15, 22.82, 22.39, 22.87, 22.48),
Glutathione = c(82.05, 81.42, 82.19, 82.33, 81.32, 81.77,
82.60, 82.67, 81.97, 82.59, 82.94, 82.36),
L.Tyrosine = c(127.22, 126.86, 127.48, 127.20, 127.19, 127.28,
127.46, 127.17, 126.59, 127.18, 127.37, 126.88),
Creatinine = c(222.12, 221.58, 222.20, 222.34, 221.52, 221.88,
222.49, 222.84, 222.06, 222.55, 222.89, 222.46),
S.Adenosylhomocysteine = c(526.09, 525.67, 526.14, 526.12, 526.02, 526.15,
525.98, 525.73, 525.31, 525.87, 525.79, 525.43),
Spermine = c(828.52, 828.25, 828.66, 828.61, 828.56, 828.65,
828.52, 828.36, 827.90, 828.40, 828.17, 827.94),
Acetylspermidine = c(1023.67, 1023.37, 1023.74, 1023.70, 1023.64, 1023.82,
1023.59, 1023.48, 1023.05, 1023.53, 1023.58, 1023.06))
2. Z-Score 标准化
# 筛选出控制组和测试组
data_control = subset(data, data[[1]] == "control")
data_test = subset(data, data[[1]] == "test")
# zscore
for (i in 2:length(colnames(data))) {
varname = colnames(data)[i]
data_control_zscore = (data_control[[varname]] - mean(data_control[[varname]])) / sd(data_control[[varname]])
data_test_zscore = (data_test[[varname]] - mean(data_control[[varname]])) / sd(data_control[[varname]])
data_control[[varname]] = data_control_zscore
data_test[[varname]] = data_test_zscore
}
data_zscore = data.frame(group = c(data_control[[1]], data_test[[1]]))
for (i in 2:length(colnames(data))) {
varname = colnames(data)[i]
data_zscore[[varname]] = c(data_control[[varname]], data_test[[varname]])
}
3. 数据格式转换
为了适配 z-score 得分图支持的数据格式,需要对原有数据格式做一步格式转化:
variable = c()
value = c()
group = c()
for (i in 2:ncol(data_zscore)) {
varname = colnames(data_zscore)[i]
variable = append(variable, rep(varname, nrow(data_zscore)))
value = append(value, data_zscore[[varname]])
group = append(group, data_zscore[[1]])
}
df = data.frame(variable = variable, value = value, group = group)
转化后的数据格式:
数据格式转换.png4. 作图
library(ggplot2)
ggplot(data = df, aes(x = variable, y = value, color = group)) +
geom_point(size = 2) +
geom_hline(yintercept = 0, linetype = 2) +
coord_flip() +
scale_color_manual(values = c("mediumseagreen", "firebrick")) +
theme_bw() +
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank()) +
labs(x = "", y = "Z-Score")
Z-Score 得分图.png