# 必要なパッケージの読み込み
library(ggplot2)
library(RColorBrewer)
library(dplyr)
## 
## 次のパッケージを付け加えます: 'dplyr'
## 以下のオブジェクトは 'package:stats' からマスクされています:
## 
##     filter, lag
## 以下のオブジェクトは 'package:base' からマスクされています:
## 
##     intersect, setdiff, setequal, union
library(ggpubr)

# 色の設定(色盲対応パレットからHSとNDの色を選択)
color_nd <- brewer.pal(9, "Reds")[8]    # ND用の赤色
color_hs <- brewer.pal(9, "Greys")[4]   # HS用の灰色

# データフレームの作成
data <- data.frame(
  Type = c(rep("HS", 6), rep("ND", 6)),   # HSとNDのラベルを指定
  Displacement = c(0.722038, 0.766816, 0.696086, 0.328322, 
                   0.579145, 0.458104, 2.097899, 3.937968, 
                   2.678359, 2.405114, 3.638056, 2.876911) # 変位データの入力
)

# グループごとの平均と標準誤差の計算
summary_data <- data %>%
  group_by(Type) %>%                    # Typeごとにデータをグループ化
  summarise(
    AVE = mean(Displacement),           # グループごとの平均を計算してAVEに格納
    SE = sd(Displacement) / sqrt(n())   # グループごとの標準誤差を計算してSEに格納
  )

# t検定の実行
t_test_result <- t.test(Displacement ~ Type, data = data, var.equal = TRUE) # 等分散を仮定したt検定
p_value <- t_test_result$p.value                                           # t検定のp値を取得

# p値に基づいた有意水準ラベルの設定
significance_label <- ifelse(p_value < 0.001, "***",
                             ifelse(p_value < 0.01, "**",
                                    ifelse(p_value < 0.05, "*", "ns"))) # "ns"は非有意を表す

# グラフの作成
p <- ggplot(summary_data, aes(x = Type, y = AVE, fill = Type)) +  
  geom_bar(stat = "identity", width = 0.5, color = "black", size = 1) +   # 平均値の棒グラフ、幅を0.5に設定
  geom_errorbar(aes(ymin = AVE - SE, ymax = AVE + SE), width = 0.25, color = "black", size = 0.8) + # エラーバー
  geom_point(data = data, aes(x = Type, y = Displacement),                # 各データ点をプロット
             shape = 21, color = "black", 
             fill = ifelse(data$Type == "HS", scales::alpha(color_hs, 0.5), scales::alpha(color_nd, 0.5)),
             position = position_jitter(width = 0.1, height = 0), size = 3) + # ドットの色を薄く、横に散らして配置
  labs(x = "", y = "Displacement [mm]", title = "Displacement Comparison with Statistical Test") + 
  scale_y_continuous(expand = c(0, 0)) +                                 # Y軸の余白をなしに設定
  coord_cartesian(ylim = c(0, 4)) +                                      # Y軸の範囲を0から4に設定
  theme_minimal(base_family = "sans") +
  theme(
    plot.background = element_rect(fill = "transparent", color = NA),    # 背景を透明に設定
    panel.background = element_rect(fill = "transparent", color = NA),
    axis.line = element_line(size = 0.8, color = "black"),               # 軸線を太く、黒に設定
    axis.ticks = element_line(color = "black", size = 0.6),              # 軸目盛り線の太さと色を設定
    axis.ticks.length = unit(-0.25, "cm"),                               # 目盛り線の長さを内向きに設定
    axis.text = element_text(size = 12, face = "bold"),                  # 軸のラベルを太字で設定
    axis.title.y = element_text(size = 14, face = "bold"),               # Y軸タイトルのフォントを太字で設定
    plot.title = element_text(size = 16, face = "bold"),                 # グラフタイトルのフォントを太字で設定
    panel.grid = element_blank()                                         # グリッド線を非表示
  ) +
  scale_fill_manual(values = c("HS" = color_hs, "ND" = color_nd)) +      # 塗りの色を手動で設定
  annotate("text", x = 1.5, y = 3.5, label = significance_label, size = 5) # 有意水準ラベルを表示
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
# プロットの表示
p