# 必要なパッケージの読み込み
library(ggplot2)
library(RColorBrewer)
library(dplyr)
## 
## 次のパッケージを付け加えます: 'dplyr'
## 以下のオブジェクトは 'package:stats' からマスクされています:
## 
##     filter, lag
## 以下のオブジェクトは 'package:base' からマスクされています:
## 
##     intersect, setdiff, setequal, union
# 色の設定(色盲対応パレットから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) %>%
  summarise(
    AVE = mean(Displacement),                      # 平均値
    SE = sd(Displacement) / sqrt(n())              # 標準誤差
  )

# 棒グラフの作成(全点プロットを追加)
p <- ggplot() +
  geom_bar(data = summary_data, aes(x = Type, y = AVE, fill = Type), 
           stat = "identity", 
           width = 0.5,                # バーの太さを指定(数値を変更して調整)
           position = position_dodge(width = 0.6), # バー間の間隔を指定
           color = "black", size = 1)+

  geom_errorbar(data = summary_data, aes(x = Type, 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, stroke = 1.5) +
  

  labs(x = "", y = "Displacement [mm]", title = "Displacement Comparison with Data Points") + # ラベルとタイトル
  scale_y_continuous(expand = c(0, 0)) +                                  # Y軸の余白をなくす
  coord_cartesian(ylim = c(0, 4)) +                                      # Y軸の下限を0に固定
  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)) +       # 塗りの色設定
  scale_color_manual(values = c("HS" = color_hs, "ND" = color_nd))        # 全点プロットの色設定
## 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
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's colour values.