Rの基本~ggplot2

Published

May 24, 2025

getwd()  # 現在の作業ディレクトリを確認
[1] "C:/Users/kpd06/OneDrive/ドキュメント/Project/2025/003_Bayse/ベイズ統計モデリング/qmd"

1 1 Rの基礎

1.1 1-15.乱数生成

#乱数の固定
set.seed(1)
rnorm(n=1, mean=0, sd=1)
[1] -0.6264538
rnorm(n=1, mean=0, sd=1)
[1] 0.1836433
set.seed(1)
rnorm(n=1, mean=0, sd=1)
[1] -0.6264538
rnorm(n=1, mean=0, sd=1) #set.seed(1)を境に、乱数の生成が繰り返される
[1] 0.1836433

1.2 1-16. forループ

  • 複数の乱数を作成する
    • rep関数で大きさnのベクトル作成
    • for文で1~n回、ランダムサンプリングして大きさnのベクトルのi番目の要素に代入
#乱数を複数回発生させ、その結果を格納していく
#rep関数で0を格納し、そのなかに乱数を格納していく
result <- rep(0,100)
set.seed(1) #乱数固定を忘れない
x <- c(1:100) # 繰り返し回数
for (i in x){
  result[i] <- rnorm(n = 1, 1, 10)
}
library(ggplot2)
result_df <- data.frame(col1 = result)
ggplot(result_df, mapping = aes(x = col1),bins = 100) +
  geom_histogram() +
  geom_density(size=1.5)

#y軸の値を..density..で明示する必要あり
graph1<-
  ggplot(result_df, aes(x = col1, y = ..density..)) +
  geom_histogram(bins = 10, fill = "lightblue", color = "black", alpha = 0.7) + # binsの数を調整
  geom_density(aes(y = ..density..), color = "red", size = 1.5) 
#乱数を複数回発生させ、その結果を格納していく
#rep関数で0を格納し、そのなかに乱数を格納していく
result_1000 <- rep(0,1000)
set.seed(1) #乱数固定を忘れない
y <- c(1:1000) # 繰り返し回数
for (i in y){
  result_1000[i] <- rnorm(n = 1, 1, 10)
}
result_1000_df <- data.frame(col1 = result_1000)
graph2<- 
  ggplot(result_1000_df, aes(x = col1, y = ..density..)) +
  geom_histogram(bins = 10, fill = "lightblue", color = "black", alpha = 0.7) + # binsの数を調整
  geom_density(aes(y = ..density..), color = "red", size = 1.5) 
#grid.arrange関数で複数のプロットを並べる
pacman::p_load(gridExtra)
grid.arrange(graph1,graph2)

2 2 記述統計

2.1 2-7. 自己共分散

Nile
Time Series:
Start = 1871 
End = 1970 
Frequency = 1 
  [1] 1120 1160  963 1210 1160 1160  813 1230 1370 1140  995  935 1110  994 1020
 [16]  960 1180  799  958 1140 1100 1210 1150 1250 1260 1220 1030 1100  774  840
 [31]  874  694  940  833  701  916  692 1020 1050  969  831  726  456  824  702
 [46] 1120 1100  832  764  821  768  845  864  862  698  845  744  796 1040  759
 [61]  781  865  845  944  984  897  822 1010  771  676  649  846  812  742  801
 [76] 1040  860  874  848  890  744  749  838 1050  918  986  797  923  975  815
 [91] 1020  906  901 1170  912  746  919  718  714  740
acf(
  Nile,
  type = "covariance",
  plot = T, #Fにするとこれログラムは出ない
  lag.max = 20
)

3 3 ggplot2によるグラフ作成

ggplot(data=data, mapping = aes(x = x_col, y= y_col))+ geom_hogehoge() で記述するのが基本 この下に、 + labs(title = ) など付加的な要素を加えていく ## 3-6. 箱ひげ図、バイオリンプロット

head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa
table(iris$Species)

    setosa versicolor  virginica 
        50         50         50 
p_box <- ggplot(data = iris,
                mapping = aes(x = Species, y = Petal.Length)) +
  geom_boxplot()+
  labs(title = "箱ひげ図")
p_violin <- ggplot(data = iris,
                mapping = aes(x = Species, y = Petal.Length)) +
  geom_violin()+
  labs(title = "バイオリンプロット")

grid.arrange(p_box, p_violin, ncol = 2)

3.1 3-7. 時系列データ

nile_df <- data.frame(
  year = 1871:1970,
  Nile = as.numeric(Nile) #ts型のデータを数値型に直す
)
ggplot(nile_df, aes(x = year, y = Nile)) +
  geom_line()

#ggfortifyを使うと楽
library(ggfortify)
autoplot(Nile)