準備

library(plotly)  # ライブラリの読み込み

基本チャート

折れ線グラフ

x <- c(1:100)
random_y <- rnorm(100, mean = 0)
data <- data.frame(x, random_y)

plot_ly(data, x = ~x, y = ~random_y, type = 'scatter', mode = 'lines')

折れ線グラフ(エラーバー)

library(plyr)

data_mean <- ddply(ToothGrowth, c("supp", "dose"), summarise, length = mean(len))
data_sd <- ddply(ToothGrowth, c("supp", "dose"), summarise, length = sd(len))
data <- data.frame(data_mean, data_sd$length)
data <- rename(data, c("data_sd.length" = "sd"))
data$dose <- as.factor(data$dose)

plot_ly(data = data[which(data$supp == 'OJ'),], x = ~dose, y = ~length, type = 'scatter', mode = 'lines+markers',
        name = 'OJ',
        error_y = ~list(value = sd,
                        color = '#000000')) %>%
  add_trace(data = data[which(data$supp == 'VC'),], name = 'VC')

グラフの重ね描き

trace_0 <- rnorm(100, mean = 5)
trace_1 <- rnorm(100, mean = 0)
trace_2 <- rnorm(100, mean = -5)
x <- c(1:100)

data <- data.frame(x, trace_0, trace_1, trace_2)

plot_ly(data, x = ~x, y = ~trace_0, name = 'trace 0', type = 'scatter', mode = 'lines') %>%
  add_trace(y = ~trace_1, name = 'trace 1', mode = 'lines+markers') %>%
  add_trace(y = ~trace_2, name = 'trace 2', mode = 'markers')

時系列(日)

today <- Sys.Date()
tm <- seq(0, 600, by = 10)
x <- today - tm
y <- rnorm(length(x))

plot_ly(x = ~x, y = ~y, mode = 'lines', text = paste(tm, "days from today"))

時系列(時間)

now_ct <- as.POSIXct(Sys.time())
tm <- seq(0, 600, by = 10)
x <- now_ct - tm
y <- rnorm(length(x))

plot_ly(x = ~x, y = ~y, mode = 'lines', text = paste(tm, "seconds from now in", Sys.timezone()))

エリアプロット

diamonds1 <- diamonds[which(diamonds$cut == "Fair"),]
density1 <- density(diamonds1$carat)

diamonds2 <- diamonds[which(diamonds$cut == "Ideal"),]
density2 <- density(diamonds2$carat)

plot_ly(x = ~density1$x, y = ~density1$y, type = 'scatter', mode = 'lines', name = 'Fair cut', fill = 'tozeroy') %>%
  add_trace(x = ~density2$x, y = ~density2$y, name = 'Ideal cut', fill = 'tozeroy') %>%
  layout(xaxis = list(title = 'Carat'),
         yaxis = list(title = 'Density'))

エリアプロット(積み上げ)

data <- t(USPersonalExpenditure)
data <- data.frame("year"=rownames(data), data)

plot_ly(data, x = ~year, y = ~Food.and.Tobacco, name = 'Food and Tobacco', type = 'scatter', mode = 'none', fill = 'tozeroy', fillcolor = '#F5FF8D') %>%
  add_trace(y = ~Household.Operation, name = 'Household Operation', fillcolor = '#50CB86') %>%
  add_trace(y = ~Medical.and.Health, name = 'Medical and Health', fillcolor = '#4C74C9') %>%
  add_trace(y = ~Personal.Care, name = 'Personal Care', fillcolor = '#700961') %>%
  add_trace(y = ~Private.Education, name = 'Private Education', fillcolor = '#312F44') %>%
  layout(title = 'United States Personal Expenditures by Categories',
         xaxis = list(title = "",
                      showgrid = FALSE),
         yaxis = list(title = "Expenditures (in billions of dollars)",
                      showgrid = FALSE))

散布図

plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, color = ~Species,
        type = "scatter", mode = "markers")

散布図(スタイル)

plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, color = ~Species,
        marker = list(size = 10)) %>%
  layout(title = 'Styled Scatter',
         yaxis = list(zeroline = FALSE),
         xaxis = list(zeroline = FALSE))

散布図(シンボル)

plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, color = I("black"),
        symbol = ~Species, symbols = c('circle','x','o'),
        type = "scatter", mode = "markers")

バブルチャート

plot_ly(
  x = c(1, 2, 3),
  y = c(5, 5.5, 7),
  type = "scatter", mode = "markers",
  size = c(1, 5, 10),
  marker = list(
    color = c("red", "blue" , "green")
    )
)

棒グラフ

plot_ly(
  x = c(1, 2, 3),
  y = c(5, 5.5, 7),
  type = "bar"
)

棒グラフ(グループ)

Animals <- c("giraffes", "orangutans", "monkeys")
SF_Zoo <- c(20, 14, 23)
LA_Zoo <- c(12, 18, 29)
data <- data.frame(Animals, SF_Zoo, LA_Zoo)

plot_ly(data, x = ~Animals, y = ~SF_Zoo, type = 'bar', name = 'SF Zoo') %>%
  add_trace(y = ~LA_Zoo, name = 'LA Zoo') %>%
  layout(yaxis = list(title = 'Count'), barmode = 'group')

棒グラフ(積み上げ)

Animals <- c("giraffes", "orangutans", "monkeys")
SF_Zoo <- c(20, 14, 23)
LA_Zoo <- c(12, 18, 29)
data <- data.frame(Animals, SF_Zoo, LA_Zoo)

plot_ly(data, x = ~Animals, y = ~SF_Zoo, type = 'bar', name = 'SF Zoo') %>%
  add_trace(y = ~LA_Zoo, name = 'LA Zoo') %>%
  layout(yaxis = list(title = 'Count'), barmode = 'stack')

棒グラフ(エラーバー)

library(plyr)

data_mean <- ddply(ToothGrowth, c("supp", "dose"), summarise, length = mean(len))
data_sd <- ddply(ToothGrowth, c("supp", "dose"), summarise, length = sd(len))
data <- data.frame(data_mean, data_sd$length)
data <- rename(data, c("data_sd.length" = "sd"))
data$dose <- as.factor(data$dose)

plot_ly(data = data[which(data$supp == 'OJ'),], x = ~dose, y = ~length, type = 'bar', name = 'OJ',
        error_y = ~list(value = sd,
                        color = '#000000')) %>%
  add_trace(data = data[which(data$supp == 'VC'),], name = 'VC')

ヒートマップ

plot_ly(z = volcano,
        type = "heatmap"
)

統計チャート

ヒストグラム

plot_ly(
  x = rchisq(100, 5, 0),
  type = "histogram"
)

ヒストグラム(重ね描き)

plot_ly(alpha = 0.6) %>%
  add_histogram(x = ~rnorm(500)) %>%
  add_histogram(x = ~rnorm(500) + 1) %>%
  layout(barmode = "overlay")

## 2Dヒストグラム

plot_ly(
  x = rchisq(100, 5, 0),
  y = rchisq(100, 5, 0),
  type = "histogram2d"
)

箱ひげ図

plot_ly(
  x = rnorm(50),
  type = "box") %>%
add_trace(x = rnorm(50))

参照


「データから価値を創造する」一般社団法人データマーケティングラボラトリー

Copyright© DML All Rights Reserved.