🎨
CHAPTER 13

高度なggplot2とカスタム可視化

データビジュアライゼーションの真の力を解き放つ。ggplot2の高度な機能とカスタマイゼーションを駆使して、美しく洞察に満ちたグラフィックを創造しよう。

🚀 ggplot2の高度な世界

基本的なggplot2の使い方を超えて、より高度なテクニックと美しいカスタマイゼーションの世界に足を踏み入れます。

📦 環境のセットアップ

高度な可視化環境の準備
# 可視化エコシステムの読み込み library(tidyverse) library(ggplot2) # 高度な可視化パッケージ library(scales) library(viridis) # サンプルデータの作成 set.seed(123) # ビジネスデータの作成 business_data <- tibble( year = rep(2020:2023, each = 12), month = rep(1:12, 4), sales = rnorm(48, 1000, 200) ) glimpse(business_data)

🎨 テーマとカスタマイゼーション

ggplot2のテーマシステムを活用して、プロフェッショナルで美しい可視化を作成します。

カスタムテーマの作成
# 基本プロット basic_plot <- business_data %>% ggplot(aes(x = month, y = sales)) + geom_line() print(basic_plot) # ミニマルテーマ minimal_plot <- basic_plot + theme_minimal() print(minimal_plot)

🌈 色彩とパレット

効果的な色の使い方と、美しいカラーパレットの活用方法を学びます。

カラーパレットの活用
# カテゴリカルデータ category_data <- business_data %>% mutate(quarter = paste("Q", ceiling(month / 3))) # カラーパレット適用 color_plot <- category_data %>% ggplot(aes(x = month, y = sales, color = quarter)) + geom_point() + scale_color_viridis_d() print(color_plot)

📊 高度なgeom層

複雑なデータ構造を効果的に表現する高度なgeometry関数を探索します。

複雑な可視化
# ヒートマップデータ heatmap_data <- business_data %>% group_by(year, month) %>% summarise(avg_sales = mean(sales), .groups = 'drop') # ヒートマップ作成 heatmap_plot <- heatmap_data %>% ggplot(aes(x = month, y = year, fill = avg_sales)) + geom_tile() print(heatmap_plot)

📝 注釈とラベリング

効果的な注釈とラベルでグラフの情報価値を最大化します。

注釈の追加
# 注釈付きプロット annotated_plot <- business_data %>% ggplot(aes(x = month, y = sales)) + geom_line() + labs( title = "月次売上推移", x = "月", y = "売上" ) print(annotated_plot)

🔄 ファセッティング

複数のサブプロットで多次元データを効果的に表示します。

ファセット可視化
# ファセットプロット facet_plot <- business_data %>% ggplot(aes(x = month, y = sales)) + geom_line() + facet_wrap(~ year) print(facet_plot)

🧩 ggplot2拡張パッケージ

ggplot2エコシステムの拡張パッケージで可視化の可能性を広げます。

拡張機能の活用
# 統計的可視化 stat_plot <- business_data %>% ggplot(aes(x = factor(year), y = sales)) + geom_boxplot() print(stat_plot) # 密度プロット density_plot <- business_data %>% ggplot(aes(x = sales)) + geom_density() print(density_plot)

🖱️ インタラクティブ可視化

静的なグラフを超えて、インタラクティブな可視化の世界を探索します。

動的可視化
# インタラクティブプロット用データ interactive_data <- business_data %>% mutate(date = as.Date(paste(year, month, "01", sep = "-"))) # 時系列プロット time_plot <- interactive_data %>% ggplot(aes(x = date, y = sales)) + geom_line() + geom_point() print(time_plot)

💼 プロフェッショナルスタイリング

ビジネス用途に適した、洗練されたプロフェッショナルなスタイルを作成します。

ビジネスグレードの可視化
# プロフェッショナルテーマ professional_plot <- business_data %>% ggplot(aes(x = month, y = sales)) + geom_col() + theme_classic() + labs( title = "年間売上レポート", subtitle = "月次パフォーマンス分析" ) print(professional_plot)

🎯 まとめ

本章では、ggplot2の高度な機能とカスタマイゼーション技術を学習しました。

これらの技術により、データの洞察を美しく、効果的に伝えることができます。

前の章
第12章: 時系列予測の魔法
次の章
第14章: インタラクティブ可視化