🌟 インタラクティブ可視化の世界
インタラクティブ可視化は、ユーザーがデータと直接対話できる強力なツールです。ズーム、パン、フィルタリングなどの機能により、データの探索が直感的になります。
📦 環境のセットアップ
library(tidyverse)
library(plotly)
library(shiny)
library(DT)
set.seed(123)
sales_data <- tibble(
date = seq(as.Date("2023-01-01"), as.Date("2023-12-31"), by = "day"),
sales = rnorm(365, 1000, 200),
region = sample(c("東京", "大阪", "名古屋"), 365, replace = TRUE)
)
glimpse(sales_data)
📊 plotlyの基本
plotlyは、R用の強力なインタラクティブ可視化ライブラリです。ggplot2オブジェクトを簡単にインタラクティブに変換できます。
basic_plot <- sales_data %>%
ggplot(aes(x = date, y = sales)) +
geom_point()
interactive_plot <- ggplotly(basic_plot)
print(interactive_plot)
direct_plot <- sales_data %>%
plot_ly(x = ~date, y = ~sales, type = "scatter", mode = "markers")
print(direct_plot)
🔍 ホバー情報とツールチップ
ホバー機能により、マウスオーバーで詳細情報を表示できます。これにより、データポイントの詳細を直感的に探索できます。
hover_plot <- sales_data %>%
plot_ly(
x = ~date,
y = ~sales,
color = ~region,
type = "scatter",
mode = "markers"
)
print(hover_plot)
🌐 3D可視化
plotlyの3D機能を使用して、多次元データを立体的に表現します。回転やズームが可能な3Dグラフを作成できます。
plot_3d_data <- sales_data %>%
mutate(
month = month(date),
day_of_year = yday(date)
)
plot_3d <- plot_3d_data %>%
plot_ly(
x = ~month,
y = ~day_of_year,
z = ~sales,
type = "scatter3d",
mode = "markers"
)
print(plot_3d)
🎬 アニメーション
時系列データや変化するデータをアニメーションで表現することで、データの動的な変化を効果的に伝えることができます。
monthly_data <- sales_data %>%
mutate(month = month(date, label = TRUE)) %>%
group_by(month, region) %>%
summarise(avg_sales = mean(sales), .groups = 'drop')
animated_plot <- monthly_data %>%
plot_ly(
x = ~region,
y = ~avg_sales,
frame = ~month,
type = "bar"
)
print(animated_plot)
📋 インタラクティブダッシュボード
Shinyを使用して、ユーザーが操作可能な本格的なダッシュボードを作成します。リアルタイムでデータを探索できる環境を構築します。
ui <- fluidPage(
titlePanel("売上ダッシュボード"),
sidebarLayout(
sidebarPanel(
selectInput("region", "地域選択", choices = unique(sales_data$region))
),
mainPanel(plotlyOutput("sales_plot"))
)
)
server <- function(input, output) {
output$sales_plot <- renderPlotly({
filtered_data <- sales_data %>%
filter(region == input$region)
p <- filtered_data %>%
plot_ly(x = ~date, y = ~sales, type = "scatter", mode = "lines")
return(p)
})
}
📊 インタラクティブテーブル
DTパッケージを使用して、フィルタリング、ソート、検索が可能なインタラクティブなデータテーブルを作成します。
basic_table <- sales_data %>%
head(100) %>%
datatable()
print(basic_table)
custom_table <- sales_data %>%
datatable(
options = list(
pageLength = 10,
searching = TRUE
)
)
print(custom_table)
🔗 Crosstalk連携
Crosstalkを使用して、複数のインタラクティブコンポーネント間でデータを連携させます。
library(crosstalk)
shared_data <- sales_data %>%
SharedData$new()
linked_plot <- shared_data %>%
plot_ly(x = ~date, y = ~sales, color = ~region)
print(linked_plot)
🚀 デプロイメント
作成したインタラクティブ可視化やダッシュボードを、様々なプラットフォームで公開・共有する方法を学びます。
output_plot <- sales_data %>%
plot_ly(x = ~date, y = ~sales, type = "scatter", mode = "lines")
htmlwidgets::saveWidget(output_plot, "sales_chart.html")
print("HTMLファイルが作成されました")
🎯 まとめ
本章では、plotlyとshinyを使用したインタラクティブ可視化の世界を探索しました。
- plotlyによる基本的なインタラクティブグラフの作成
- ホバー情報とツールチップの活用
- 3D可視化とアニメーション技術
- Shinyダッシュボードの構築
- インタラクティブデータテーブルの実装
- コンポーネント間の連携機能
- 作品の公開と共有方法
これらの技術により、ユーザーが直感的に操作できる魅力的なデータ体験を提供できます。