do <- read.csv("C:/Users/takum/Downloads/electric_power.csv")
set.seed(2)
y1 <- rnorm(n = 10, mean =  0, sd = 1) 
y2<- rnorm(n = 10, mean =  2, sd = 2) 
d <- round(data.frame(y1, y2), 2)
fr <- as.POSIXct("2021-05-01 00:00:00") 
to <- as.POSIXct("2021-05-01 23:00:00") 
nr <- nrow(d) 
nc <- ncol(d)
px <- seq(fr, to, by = "hours")
d0=read.csv(file = "C:/Users/takum/Downloads/electric_power.csv")
 d= data.frame(y1=d0$y1, y2=d0$y2)
  px=as.POSIXct(d0$X)
COL <- c(rgb(255,   0,   0,  255, max = 255), # 赤
         rgb(  0,   0, 255,  255, max = 255), # 青
         rgb(  0, 155,   0,  255, max = 255)) # 緑
matplot(x = px, y = d, type = "l")

matplot(x = px, y = d, type = "o", pch = colnames(d))

# 作図
matplot(x = px, y = d, type = "o", pch = 1:nc, xlab = "2021", ylab = "値")

# 格子線
#(線が目盛りからずれときがあるため,後述のabline関数を利用を推奨)
grid()

# 凡例(はんれい)
# 表示場所はtop(上)/bottom(下)とright(右)/left(左)を組み合わせて決める。
# 例)topleft(左上)
legend("bottomright", legend = colnames(d), pch = 1:nc, lty = 1:nc, col = 1:nc)

library(plotly)
##  要求されたパッケージ ggplot2 をロード中です
## 
##  次のパッケージを付け加えます: 'plotly'
##  以下のオブジェクトは 'package:ggplot2' からマスクされています:
## 
##     last_plot
##  以下のオブジェクトは 'package:stats' からマスクされています:
## 
##     filter
##  以下のオブジェクトは 'package:graphics' からマスクされています:
## 
##     layout

matplot(x = px,            
        y = d,             
        type = "n",        
        main = "電気使用量データ",
        xlab = "時間",
         ylab = "電力(W)")

px.g <- seq(px[1], px[nr], by = 60*60*2*2)


abline(lty=5, col=gray(0.5, 0.25), h=seq(-40, 40,6), v=px.g)

matlines(x = px, y = d$y1, type = "o", pch = 1, lty = 1, col = COL[1])
matlines(x = px, y = d$y2, type = "o", pch = 2, lty = 2, col = COL[2])


legend("topright", col = COL, pch = 1:nc, lty = 1:nc, legend = colnames(d))