COLS <- c(rgb(1, 0, 1, .25), # ピンク
          rgb(0, 0, 1, .25), # ラベンダー
          rgb(0, 1, 0, .25)) # グリーン
GRAY <- rgb(.5, .5, .5, .25) # グレー
d <- read.csv("C:/Users/takum/Downloads/QQplot (1).csv")
set.seed(10)

n = 100
xn = rnorm(n)
xt = rt(n,df = 10)
summary(data.frame(d$x))
##       d.x         
##  Min.   :-1.5920  
##  1st Qu.:-0.5965  
##  Median :-0.0520  
##  Mean   : 0.0021  
##  3rd Qu.: 0.5490  
##  Max.   : 2.1810
bins = seq(-3,4,1)

hist(d$x,breaks = bins, col = COLS[1],
     main = 'ヒストグラム1',
     xlab = '観測値',
     ylab = '度数')

p <- function(k) (k - 0.5) / n
k <- 1:n
q <- qnorm(p = p(k))

x <- seq(-1.59, 2.18, 0.1)
y <- dnorm(x)
matplot(x, y, type = 'l')
abline(v = q, col = COLS[1])

xn.o <- xn[order(xn)]
d$x.o <- d$x[order(d$x)]


matplot(x = q, y = xn.o, type = 'n',
        xlim = c(-4, 4),
        ylim = c(-4, 4),
        main = '正規Q-Qプロット',
        xlab = '正規分位数',
        ylab = '標本分位数')
matpoints(x = q, y = xn.o, pch = 16, col = COLS[1])
matpoints(x = q, y = d$x.o, pch = 16, col = COLS[2])
abline(a = 0, b = 1, col = 'red')
legend('topleft', col = COLS[1:2], pch = 16,
       legend = c('標本1(正規乱数)', '標本2(x)'))

library(car)
##  要求されたパッケージ carData をロード中です
qqPlot(d$x,
       main = 'N(0, 1)',
       xlab = '正規分位数',
       ylab = '標本分位数')

## [1] 75 78