Rで解析:ggplot2でjoyplotを作成「ggjoy」パッケージ

joyplotは多数の分布の変化を把握するのに便利な表現方法だと考えます。そんな、joyplotをggplot2で作成できるパッケージの紹介です。

本パッケージの利用方法ですが、データを用意しggplotコマンドにgeom_joyまたはgom_joy2コマンドを加えるだけです。直感的に使用できると考えます。

紹介では気象庁から22.05.03の各都道府県の最高気温を0:00から23:00まで1時間毎に取得し、joyplotを作成しました。

なお、本パッケージは記事作成時に入手可能ですが「ggridges」パッケージへの移行が推奨されています。

パッケージバージョンは0.4.1。実行コマンドはwindows 11のR version 4.1.2で確認しています。

パッケージのインストール

下記コマンドを実行してください。

#パッケージのインストール
install.packages("ggjoy")

気象庁から最高気温を取得

最高気温はNewMaxTempに格納しています。

#「tidyverse」パッケージを読み込み
if(!require("tidyverse", quietly = TRUE)){
  install.packages("tidyverse");require("tidyverse")
}
#都道府県を準備
JpanPref <- c("北海道", "青森県", "岩手県", "宮城県", "福島県", "茨城県", "千葉県",
              "秋田県", "山形県", "新潟県", "栃木県", "埼玉県", "東京都", "群馬県",
              "山梨県", "神奈川県", "富山県", "長野県", "静岡県", "石川県", "福井県",
              "岐阜県", "愛知県", "滋賀県", "三重県", "京都府", "奈良県", "和歌山県",
              "兵庫県", "大阪府", "鳥取県", "岡山県", "島根県", "広島県", "香川県",
              "徳島県", "愛媛県", "高知県", "山口県", "福岡県", "大分県", "宮崎県",
              "佐賀県", "熊本県", "鹿児島県", "長崎県", "沖縄県")
#時間文字列を作成
Hour <- paste0(formatC(0:23, width = 2, flag = "0"), "00")
#データ保管用変数
NewMaxTemp <- data.frame()
for(i in seq(Hour)){
  ###気象庁より20220503の毎時の最高気温を取得#####
  #参考:https://www.data.jma.go.jp/obd/stats/data/mdrr/docs/csv_dl_readme.html
  MaxTemp <- read.csv(paste0("https://www.data.jma.go.jp/obd/stats/data/mdrr/tem_rct/alltable/mxtemsadext00_20220503", Hour[i], ".csv"),
                         header = T, fileEncoding = "cp932")
  #最高気温処理
  GetMaxTemp <- NULL
  for(n in 1:47){
    #都道府県を抽出
    GetPrefData <- MaxTemp[which(MaxTemp[, 2] %in% grep(JpanPref[n], MaxTemp[, 2], value = TRUE)),]
    #最高気温を降順で並び替え
    GetPrefData <- GetPrefData[order(GetPrefData[, 10], decreasing = TRUE),]
    #最高気温を取得
    GetMaxTemp <- c(GetMaxTemp, GetPrefData[1, 10])
  }

  HourTemp <- cbind(Hour[i], JpanPref, GetMaxTemp)
  NewMaxTemp <- rbind(NewMaxTemp, HourTemp)

}
#列名を付与
colnames(NewMaxTemp) <- c("Hour", "Pref", "MaxTemp")
#最高気温を数値化
NewMaxTemp[, 3] <- type.convert(NewMaxTemp[, 3], as.is = TRUE)
#最高気温で都道府県を並び替え
#準備
NewMaxTemp %>%
  group_by(Pref) %>%
  summarise(Max = max(MaxTemp)) %>%
  arrange(Max) %>%
  mutate(Pref = factor(Pref)) %>%
  select(Pref) -> OrderVecPref
#並び替え
NewMaxTemp %>%
  mutate(Pref = factor(Pref, levels = OrderVecPref$Pref)) -> NewMaxTemp

コマンドの紹介

詳細はコマンド、パッケージのヘルプを確認してください。体裁はggplot2のコマンドで調整可能です。

#パッケージの読み込み
library("ggjoy")
#joyplotの作成
ggplot(NewMaxTemp, aes(x = MaxTemp, y = Pref, fill = Pref)) +
  geom_joy(show.legend = F) +
  geom_vline(xintercept = 26.6, col = "#ffc1c1") +
  theme(axis.text.y = element_text(size = 7.5),
        axis.text = element_text(colour = "#ffffe0"),
        panel.grid = element_blank(),
        panel.background = element_rect(fill = "#0a0a0a"),
        plot.background = element_rect(fill = "#0a0a0a"),
        plot.margin = unit(rep(0.2, 4), "cm"))

出力例

・joyplotの作成

少しでも、あなたの解析が楽になりますように!!

Prices and shipping availability may change. Please refer to the product page at time of purchase.
Content displayed on this site is provided by Amazon and may be updated or removed.
Amazon Associate, karada-good earns income through qualifying sales.
タイトルとURLをコピーしました