R/plotlyの忘備録


良く忘れるのでメモ。

データ

UCI machine learning repositoryのWater Treatment Plantのデータ。とりあえず標準化。

library(plotly)
library(lubridate)
library(dplyr)

# https://archive.ics.uci.edu/ml/datasets/water+treatment+plant
X <- read.csv(file="water-treatment.data", header=FALSE, na.strings="?")
head(X)
X <- na.omit(X)
X <- X %>% mutate(V1=dmy(X$V1)) %>% arrange(V1)
head(X)
X <- X %>% mutate_if(is.numeric, scale) %>% mutate_if(is.numeric, as.numeric)
head(X)

Line-Plots

# line-plot
fig <- plot_ly(x=X$V1, y=X$V2, type="scatter", mode="lines", name="V2")
fig <- fig %>% add_trace(x=X$V1, y=X$V3, name="V3")
fig <- fig %>% add_trace(x=X$V1, y=X$V4, name="V4")
fig <- fig %>% add_trace(x=X$V1, y=X$V5, name="V5")
fig <- fig %>% layout(
  xaxis=list(title="Date"),
  yaxis=list(title="Value"))
fig

ヒストグラム

# Histogram
fig <- plot_ly(x=X$V2, type="histogram")
fig <- fig %>% layout(
  xaxis=list(title="V2"),
  yaxis=list(title="Count")
)
fig