R Code for Regional Analysis: Visualizing Open Human Mobility Data with Leaflet 地域分析のためのRコード: Leafletを用いた人流オープンデータ可視化
Abstract
地域分析のためのRコード: Leafletを用いた人流オープンデータ可視化 概要 本コードは、国土交通省が公開している「1kmメッシュ別滞在人口データ」をR言語で地図上に可視化するための、外部ライブラリへの依存を最小限に抑えた軽量なプログラムである。 新型コロナウイルス対策や観光振興政策などの地方自治体における個別政策の推進において、人流データの活用は不可欠である。しかし、従来のRを用いたメッシュデータの処理には、Rtoolsの導入やjpmesh等の特定のパッケージへの依存が必要となるケースが多く、IT環境に制約のある自治体実務者にとって導入の障壁となっていた。 本スクリプトは、JIS X 0410に基づく地域メッシュコードの座標変換を純粋な算術演算(decode_mesh3関数)によって実装しており、leafletパッケージのみでインタラクティブなマップ生成を可能にする。実際の公開コードとしては、コロナ禍以前(2019年1月)の長野県飯田市周辺の休日・昼間滞在人口を可視化するRプログラムである。 背景と謝辞 本スクリプトは、以下の書籍で紹介されているメッシュデータ可視化の考え方に着想を得て作成したものである。 書誌情報: 藤井亮輔・鈴木康司 (2025). 『超入門!Rでできるビジュアル統計学:学会・論文発表に役立つデータ可視化マニュアル』 (第2版). 金芳堂. 同書は、Rを用いた統計可視化の基礎から応用までを網羅した非常に優れたガイドであり、本スクリプトの開発にあたっても多大な示唆を得た。 本スクリプトの目的と経緯 同書のコード(190頁以降)をベースとしつつ、以下の2点を主眼に置いた代替スクリプトとして開発した。 環境依存の排除: パッケージの仕様変更への対応として、jpmeshパッケージやRtoolsを利用せず、算術演算のみでメッシュデコードを行うロジックを採用。 インタラクティブ性の向上: 静的な地図描画から、Web地図(Leaflet)を用いた拡大・縮小が可能な動的表示への拡張。 当初、同書のコードはパッケージのアップデートにより動作しない状態であったが、筆者の問い合わせを経て、現在は発行元のサイトに修正コードが掲載されている。本スクリプトは、その解決プロセスにおいて独自に生成AI(Gemini)のコード支援を受け、コードの実装を行ったものである。 本コードは、南山大学でのゼミ(プロジェクト研究)において、地域分析やオープンデータ利活用の教育資料として共有されたものの一つである。コードの汎用性と安定性を精査し、背景・関連情報を含めて整理した上で、広く一般の地域分析に資するために公開するものである。 教育的利用に関するアドバイス 本スクリプトが用いるデータの入手先(G空間情報センター)では、2019年から2021年までの人流オープンデータが継続的に公開されている。本サンプルで使用している「2019年1月(コロナ禍前)」のデータと、2020年・2021年の同月データを比較することで、感染症流行が地域社会の活力や移動習慣に与えた影響を、学生自らが定量的に分析・検証することが可能である。同センターが提供する地理空間データは、この人流オープンデータ含め、ほとんどが無料であるが、ダウンロードには事前のID登録が必要である。 データの入手方法 (Data Acquisition) 本スクリプトで使用する人流データは、G空間情報センターにおいて国土交通省からオープンデータとして公開されているものである。以下の手順で分析対象となる長野県のデータを取得されたい。 入手先URL https://www.geospatial.jp/ckan/dataset/mlit-1km-fromto 分析用データダウンロード手順 上記のG空間情報センターのページにアクセスする。 データ一覧の中から、「長野県」(または該当する都道府県名)のセクションを探す。 リソースリストの中から 「monthly_mdp_mesh1km_20(長野県)」を開き、圧縮ファイルである monthly_mdp_mesh1km_20.zip をダウンロードする。 ダウンロードしたZIPファイルを展開(解凍)し、中にあるCSVファイルを取り出す。 取り出したCSVファイルを、本スクリプトと同じ作業ディレクトリに保存する。 注意: 本スクリプトは、読み込むファイル名が monthly_mdp_mesh1km.csv であることを前提としている。したがって、展開したCSVファイルを monthly_mdp_mesh1km.csv へリネームするか、スクリプト内の read.csv() の引数を書き換える必要がある。 データの定義 (Data Definition) 本スクリプトで扱うデータは、国土交通省「1kmメッシュ別滞在人口データ」に基づいている。主要な項目の定義は以下の通りである。 mesh1kmid(第3次地域メッシュコード): JIS X 0410に基づき、日本全国を約1km四方の格子状に区切った8桁のコードである。 population(換算人口値): スマートフォンのGPSログ等から算出された滞在人口の推計値である。 特定の1時間において、その場所に1時間滞在した人を1人、15分滞在した人を0.25人とカウントする「時間按分」が行われている。 サンプル値から日本全体の総人口規模に拡大推計された数値である。 dayflag(曜日区分): データの集計対象となる日の区分である。 0: 休日(土曜・日曜・祝日および1月2日、1月3日、12月29日〜12月31日)。 1: 平日(休日以外の月曜〜金曜)。 timezone(時間帯区分): 1日のうち集計対象となる時間帯の区分である。 0: 昼間(11時台〜14時台の平均値)。 1: 夜間(21時台〜24時台の平均値)。 2: 全日(0時台〜23時台の平均値)。 本スクリプトにおける抽出条件 本スクリプト同梱のサンプルおよび可視化対象は、以下の条件で抽出されたものである。 地図の初期表示: 実行時に可視化対象である長野県飯田市周辺(飯田駅付近)が自動的にズーム表示されるよう、setView 関数によって初期座標が設定されている。 対象年月: 2019年1月(新型コロナウイルス流行前のベースライン)。 曜日区分: 休日(dayflag == 0)。 時間帯区分: 昼間(timezone == 0)。 描画フィルタ: 視認性向上のため、換算人口値が100人未満のメッシュは描画対象から除外している。 【関連リソース(将来推計人口の可視化)】 本スクリプト(CSVを用いた動的な人流データの可視化)と併せて、地域の長期的な構造変化を捉えるためのツールとして、シェープファイルを用いた「将来推計人口メッシュ」の可視化コードも公開している。分析の目的に応じて、以下の関連リソースも活用されたい。 関連レコード: R Code for Regional Analysis: Interactive Visualization of Future Population Projection Mesh Data with Leaflet DOI: https://doi.org/10.5281/zenodo.19806460 [English] R Code for Regional Analysis: Visualizing Open Human Mobility Data with Leaflet Abstract This repository provides a lightweight R script designed to visualize "1km Mesh Stay Population Data" published as open data by the Ministry of Land, Infrastructure, Transport and Tourism (MLIT) of Japan. The utilization of human mobility data is indispensable for the advancement of specific policies by local governments, such as COVID-19 countermeasures and tourism promotion. However, traditional methods for processing mesh data in R often require the installation of Rtools or rely on specific packages like jpmesh. These requirements frequently act as technical barriers for practitioners in administrative environments with restricted IT settings. This script addresses these issues by implementing regional mesh code coordinate conversion (based on JIS X 0410) through pure arithmetic operations (the decode_mesh3 function), enabling the generation of interactive maps using only the leaflet package. As a case study, the provided code visualizes the holiday/daytime stay population around Iida City, Nagano Prefecture, in January 2019, representing a pre-pandemic baseline. Background and Acknowledgments This script was inspired by the visualization concepts introduced in the following publication: Reference: Ryosuke Fujii & Koji Suzuki (2025). Visual Statistics with R: A Manual for Data Visualization Useful for Academic and Paper Presentations (2nd Edition). Kinpodo. The aforementioned book is an excellent guide covering everything from the basics to the applications of statistical visualization in R, and provided significant inspiration for the development of this script. Purpose and Development History While based on the concepts from the book (specifically from page 190 onwards), this script was developed as an alternative implementation focusing on the following two objectives: Environmental Independence: To remain resilient against package updates, the script adopts a logic that performs mesh decoding through arithmetic operations alone, eliminating the need for jpmesh or Rtools. Enhanced Interactivity: The script extends static mapping into dynamic, interactive Web maps (Leaflet) that allow for seamless zooming and panning. Initially, the original code in the book became non-functional due to package updates. Following inquiries and feedback from the author of this script to the original writers and publisher, corrected code is now available on the publisher's website. This script was developed by independently utilizing code assistance from generative AI (Gemini) during the troubleshooting process to build an interactive implementation using the Leaflet package. These materials are shared as educational resources for regional analysis and open data utilization within seminars and project research at Nanzan University. This specific code has been refined for general use, organized with relevant background information, and published to contribute to the field of regional analysis. Educational Note for Comparative Analysis Open human mobility data from 2019 to 2021 is consistently available at the same source (G-Spatial Information Center). By comparing the "January 2019 (pre-pandemic)" baseline used in this script with data from 2020 and 2021, users can quantitatively analyze and verify the impact of the pandemic on regional vitality and mobility patterns. Most of the geospatial data provided by the center, including the human flow open data, is available free of charge. However, prior user registration is required for downloading. Data Acquisition The human mobility data used in this script is provided as open data by the Ministry of Land, Infrastructure, Transport and Tourism (MLIT) via the G-Spatial Information Center. Source URL https://www.geospatial.jp/ckan/dataset/mlit-1km-fromto Steps to Download Access the URL above. Locate the section for "Nagano Prefecture" (長野県). Select "monthly_mdp_mesh1km_20 (Nagano Prefecture)" and download the compressed file monthly_mdp_mesh1km_20.zip. Extract (unzip) the downloaded ZIP file to obtain the CSV file. Place the extracted CSV file in the same directory as the script. Note: The script is configured to read a file named monthly_mdp_mesh1km.csv. Please rename the extracted CSV file or update the read.csv() function in the script accordingly. Data Definition Initial Map View: The script is configured via the setView function to automatically zoom in on the area around Iida City, Nagano Prefecture (near Iida Station), upon execution. mesh1kmid: 8-digit 3rd-level regional mesh code. population: Converted human mobility value estimated through time-apportionment and expansion based on the total population of Japan. Policy Significance: The utilization of human mobility data is indispensable for the advancement of specific policies by local governments, such as COVID-19 countermeasures and tourism promotion. Visual Clarity and Data Masking: To enhance visual clarity and eliminate minor noise on the map, mesh data with values less than 100 are hidden. This allows for an analysis focused on significant movement patterns, while also serving to ensure statistical reliability and privacy protection. Filters: January 2019, Holidays (dayflag="0"), Daytime (timezone="0": average from 11:00 AM to 2:00 PM). [Related Resource: Future Population Projection] In addition to this script (dynamic human mobility visualization using CSV), we have also published a script for visualizing "Future Population Projection Mesh Data" using shapefiles to analyze long-term structural changes in regions. Please utilize the following related resource according to your analytical purposes. DOI: https://doi.org/10.5281/zenodo.19806460
Publication details
- Journal / source
- Zenodo (CERN European Organization for Nuclear Research)
- Publication date
- 2 May 2026
- Language
- EN