library(tidyverse);library(ggplot2);library(grid);library(gridExtra)
1. Data setup
set.seed(123); x=rnorm(30,100,20);ID=1:10
z=(x-mean(x))/sd(x) #z transformation
x_scaled=x %>% scale() # scaling transformation
dat=cbind(ID,x,z,x_scaled)
dat=data.frame(dat)
names(dat)=c("ID","x","z_score","x_scaled")
dat
## ID x z_score x_scaled
## 1 1 88.79049 -0.5232985 -0.5232985
## 2 2 95.39645 -0.1866137 -0.1866137
## 3 3 131.17417 1.6368622 1.6368622
## 4 4 101.41017 0.1198863 0.1198863
## 5 5 102.58575 0.1798022 0.1798022
## 6 6 134.30130 1.7962422 1.7962422
## 7 7 109.21832 0.5178431 0.5178431
## 8 8 74.69878 -1.2415080 -1.2415080
## 9 9 86.26294 -0.6521193 -0.6521193
## 10 10 91.08676 -0.4062648 -0.4062648
## 11 1 124.48164 1.2957653 1.2957653
## 12 2 107.19628 0.4147858 0.4147858
## 13 3 108.01543 0.4565354 0.4565354
## 14 4 102.21365 0.1608374 0.1608374
## 15 5 88.88318 -0.5185744 -0.5185744
## 16 6 135.73826 1.8694796 1.8694796
## 17 7 109.95701 0.5554915 0.5554915
## 18 8 60.66766 -1.9566293 -1.9566293
## 19 9 114.02712 0.7629319 0.7629319
## 20 10 90.54417 -0.4339188 -0.4339188
## 21 1 78.64353 -1.0404567 -1.0404567
## 22 2 95.64050 -0.1741751 -0.1741751
## 23 3 79.47991 -0.9978288 -0.9978288
## 24 4 85.42218 -0.6949706 -0.6949706
## 25 5 87.49921 -0.5891105 -0.5891105
## 26 6 66.26613 -1.6712928 -1.6712928
## 27 7 116.75574 0.9020011 0.9020011
## 28 8 103.06746 0.2043533 0.2043533
## 29 9 77.23726 -1.1121295 -1.1121295
## 30 10 125.07630 1.3260734 1.3260734
mean(x);mean(z);mean(x_scaled)
## [1] 99.05792
## [1] -1.709425e-17
## [1] -1.709425e-17
sd(x);sd(z);sd(x_scaled)
## [1] 19.62061
## [1] 1
## [1] 1
2. Density plot
p1=ggplot(dat,aes(x=x,alpha=0.5))+geom_density()
p2=ggplot(dat,aes(x=z_score,alpha=0.5))+geom_density()
p3=ggplot(dat,aes(x=x_scaled,alpha=0.5))+geom_density()
grid.arrange(p1,p2,p3)

End