🚀 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の高度な機能とカスタマイゼーション技術を学習しました。
- カスタムテーマとスタイリングの作成
- 効果的なカラーパレットの活用
- 高度なgeometry関数の使用
- 注釈とラベリングの技術
- ファセッティングによる多次元表示
- プロフェッショナルな可視化の作成
これらの技術により、データの洞察を美しく、効果的に伝えることができます。