準備
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.