ADP (R)

[R] 15분 전력 사용량 요일별 히트맵 샘플 코드

멋쟁이천재사자 2022. 9. 21. 15:42

library(tidyverse)
library(lubridate)
library(gplots)
library(RColorBrewer)

# 데이터 가정
data.frame(tt='2010-01-01 00:00:00', power.consumption=99) %>%
rbind(data.frame(tt='2010-01-01 00:15:00', power.consumption=100)) %>%
rbind(data.frame(tt='2010-01-01 00:30:00', power.consumption=130)) %>%
rbind(data.frame(tt='2010-01-02 00:00:00', power.consumption=100)) %>%
rbind(data.frame(tt='2010-01-02 00:15:00', power.consumption=110)) %>%
rbind(data.frame(tt='2010-01-02 00:30:00', power.consumption=120)) %>%
rbind(data.frame(tt='2010-01-08 00:00:00', power.consumption=80)) %>%
rbind(data.frame(tt='2010-01-08 00:15:00', power.consumption=105)) %>%
rbind(data.frame(tt='2010-01-08 00:30:00', power.consumption=120)) -> temp

#요일 시간대 평균으로 처리
temp %>% 
mutate(wd=wday(tt,label=T,abbr=F)) %>% 
mutate(hhmm=str_sub(tt,12,16)) %>% 
group_by(wd,hhmm) %>% 
summarize(power.consumption=mean(power.consumption)) %>%
pivot_wider(names_from = hhmm,
            values_from = power.consumption,
            values_fill =0)  %>%
ungroup() %>% 
column_to_rownames(var = "wd") %>% 
as.matrix() %>% 
heatmap.2(col=brewer.pal(9,"Blues"),
  dend="none",trace="none",key=FALSE,
  margins=c(10,7), 
  cexRow=1.5,cexCol=1.2,
  colRow=c("green4","maroon"),
  main="Title Heare")

'ADP (R)' 카테고리의 다른 글

ADP 실기 26회 후기(문제 포함)  (0) 2022.09.25
오늘의 ADP 준비 / 2022-09-24 토  (2) 2022.09.24
오늘의 ADP 준비 / 2022-09-20 화(D5)  (2) 2022.09.20
[R] plot las=1  (2) 2022.09.19
[R] plot type="b"  (2) 2022.09.19