日本の気候マップの描画

Read this page in English (EN)

tmapパッケージを使用して、日本の気候マップを描画します。 rnaturalearthパッケージは、日本地図を描画するために使用します。

データの読み込み

このリポジトリの解析では、環境変数 PROJECT_DATA_DIR にデータの保存先を指定しています。 必要に応じて、適宜変更してください。

data_dir <- Sys.getenv("PROJECT_DATA_DIR")
sf_climate <- readRDS(file.path(
  data_dir,
  "climate_mesh_data_joined/climate_mesh_data_with_aridity_index.rds"
))

Evapotranspiration (PET)の描画

日本地図を描画するには、rnaturalearthhiresパッケージが必要です。

tmapの描画は、ggplot2の描画のように、レイヤを重ねる形で行います。 まず、日本地図を描画し、その上に気候変数を描画します。 その後、レイアウトを調整して完成です。

保存したい場合は、tmオブジェクトをtmap_save()関数に渡すことで、描画を保存できます。

# renv::install("ropensci/rnaturalearthhires")
variable <- "PET_year"
japan <- ne_states(country = "Japan", returnclass = "sf")
sf_plot <- sf_climate[!is.na(sf_climate[[variable]]), ]
tm <- tm_shape(japan, crs = 4612) +
  tm_polygons(col = "gray90") +
  tm_shape(sf_plot[, variable], crs = 4612) +
  tm_fill(
    fill = variable,
    fill.scale = tm_scale_continuous(values = "viridis"),
    fill.legend = tm_legend(
      title = variable,
      reverse = TRUE,
      position = c("right", "bottom"),
      frame = FALSE,
      bg.color = "transparent"
    )
  ) +
  tm_layout(frame = FALSE, bg.color = "transparent")
print(tm)

output_dir <- "output"
dir.create(output_dir, showWarnings = FALSE, recursive = TRUE)
tmap_save(
  tm,
  filename = file.path(
    output_dir,
    paste0("japan_climate_map_", variable, ".png")
  ),
  dpi = 300
)

結果は以下のようになります。

PETの描画結果|出典:国土交通省国土数値情報ダウンロードサイト URL: https://nlftp.mlit.go.jp/ksj/gml/datalist/KsjTmplt-G02-v3_0.html
ノート描画関数
  • tm_polygons()は、ポリゴンを描画するための関数です。境界線と塗りつぶしの両方を描画します。
  • tm_fill()は、塗りつぶしのみを描画するための関数です。境界線は描画しません。
  • tm_borders()は、境界線のみを描画するための関数です。塗りつぶしは描画しません。

今回のようにポリゴンが小さく、多い場合は、境界線を書いてしまうと塗りつぶしが見えなくなってしまうことがあります。 そのため、今回はtm_fill()を使用して、塗りつぶしのみを描画します。

Aridity Indexの描画

次に、Aridity Indexを描画します。

variable <- "aridity_index"
japan <- ne_states(country = "Japan", returnclass = "sf")
sf_plot <- sf_climate[!is.na(sf_climate[[variable]]), ]
tm <- tm_shape(japan, crs = 4612) +
  tm_polygons(col = "gray90") +
  tm_shape(sf_plot[, variable], crs = 4612) +
  tm_fill(
    fill = variable,
    # Low AI (drier) is yellow; high AI (wetter) is purple.
    fill.scale = tm_scale_continuous(values = hcl.colors(100, "Viridis", rev = TRUE)),
    fill.legend = tm_legend(
      title = "AI: low = dry, high = humid",
      reverse = TRUE,
      position = c("right", "bottom"),
      frame = FALSE,
      bg.color = "transparent"
    )
  ) +
  tm_layout(frame = FALSE, bg.color = "transparent")
print(tm)

tmap_save(
  tm,
  filename = file.path(
    output_dir,
    paste0("japan_climate_map_", variable, ".png")
  ),
  dpi = 300
)

結果は以下のようになります。

Aridity Indexの描画結果|出典:国土交通省国土数値情報ダウンロードサイト URL: https://nlftp.mlit.go.jp/ksj/gml/datalist/KsjTmplt-G02-v3_0.html

黄色ほどAIが小さく乾燥側、紫色ほどAIが大きく湿潤側を表します。

月ごとのAridity Indexの描画

月ごとのAridity Indexも同様に描画できます。

japan <- ne_states(country = "Japan", returnclass = "sf")
month_labels <- month.abb
for (month in seq_along(month_labels)) {
  month_label <- month_labels[month]
  variable <- paste0("aridity_index_", month_label)
  sf_plot <- sf_climate[!is.na(sf_climate[[variable]]), ]
  tm <- tm_shape(japan, crs = 4612) +
    tm_polygons(col = "gray90") +
    tm_shape(sf_plot[, variable], crs = 4612) +
    tm_fill(
      fill = variable,
      fill.scale = tm_scale_continuous(
        values = hcl.colors(100, "Viridis", rev = TRUE)
      ),
      fill.legend = tm_legend(
        title = paste0(variable, ": low = dry, high = humid"),
        reverse = TRUE,
        position = c("right", "bottom"),
        frame = FALSE,
        bg.color = "transparent"
      )
    ) +
    tm_layout(
      frame = FALSE,
      bg.color = "transparent"
    )
  print(tm)

  tmap_save(
    tm,
    filename = file.path(
      output_dir,
      paste0("japan_climate_map_", variable, ".png")
    ),
    dpi = 300
  )
}

実行すると、各月の地図がPNG形式で保存されます。