728x90
[R] ggplot2 그래프 분할(gridExtra)
1 plot 그래프 는 par로 그래프 분할
아시다 시피 R 기본 그래프인 plot으로 그릴 때는 par()를 이용해서 화면을 분할하여 그래프를 띄웁니다.
2 ggplot2 그래프 분할은 grid.arrange()로 그래프 분할
우리가 만들고자 하는 화면이 위와 같을 때, 다음처럼 설정할 수 있습니다. 1 에는 bitcoin Envelope 2 에는 bitcoin 볼린저 밴드 3 에는 기본 bitcoin 그래프
3 사용 코드 (data load)
#load input data.
source(paste0(getwd(),'/../../bitcoin/config/formarkdown_config.R'))
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(gridExtra, verbose=F,quietly=T, warn.conflicts=F)
library(ggplot2, verbose=F,quietly=T, warn.conflicts=F)
library(scales, verbose=F,quietly=T, warn.conflicts=F)
library(lubridate, verbose=F,quietly=T, warn.conflicts=F)
res<-callBTCWON(callConnection(), "2018-10-15",NA) # bitcoin rawdata
res[['candleDT']]<-1:nrow(res) # index
res[['candleDateTime2']]<-as.POSIXct(res$candleDateTime, format = "%Y-%m-%dT%H:%M") # character to date
head(res, 50)
## code candleDateTime timestamp unit
## 1 CRIX.UPBIT.KRW-BTC 2018-10-15T00:00:00+00:00 1539561660246 1
## 2 CRIX.UPBIT.KRW-BTC 2018-10-15T00:01:00+00:00 1539561719691 1
## 3 CRIX.UPBIT.KRW-BTC 2018-10-15T00:02:00+00:00 1539561779349 1
## 4 CRIX.UPBIT.KRW-BTC 2018-10-15T00:03:00+00:00 1539561840021 1
## 5 CRIX.UPBIT.KRW-BTC 2018-10-15T00:04:00+00:00 1539561900205 1
## 6 CRIX.UPBIT.KRW-BTC 2018-10-15T00:05:00+00:00 1539561960143 1
## 7 CRIX.UPBIT.KRW-BTC 2018-10-15T00:06:00+00:00 1539562019642 1
## 8 CRIX.UPBIT.KRW-BTC 2018-10-15T00:07:00+00:00 1539562079059 1
## 9 CRIX.UPBIT.KRW-BTC 2018-10-15T00:08:00+00:00 1539562140219 1
## 10 CRIX.UPBIT.KRW-BTC 2018-10-15T00:09:00+00:00 1539562198748 1
## 11 CRIX.UPBIT.KRW-BTC 2018-10-15T00:10:00+00:00 1539562259733 1
## 12 CRIX.UPBIT.KRW-BTC 2018-10-15T00:11:00+00:00 1539562319945 1
## 13 CRIX.UPBIT.KRW-BTC 2018-10-15T00:12:00+00:00 1539562377820 1
## 14 CRIX.UPBIT.KRW-BTC 2018-10-15T00:13:00+00:00 1539562439360 1
## 15 CRIX.UPBIT.KRW-BTC 2018-10-15T00:14:00+00:00 1539562499179 1
## 16 CRIX.UPBIT.KRW-BTC 2018-10-15T00:15:00+00:00 1539562559607 1
## 17 CRIX.UPBIT.KRW-BTC 2018-10-15T00:16:00+00:00 1539562618652 1
## 18 CRIX.UPBIT.KRW-BTC 2018-10-15T00:17:00+00:00 1539562680988 1
## 19 CRIX.UPBIT.KRW-BTC 2018-10-15T00:18:00+00:00 1539562738857 1
## 20 CRIX.UPBIT.KRW-BTC 2018-10-15T00:19:00+00:00 1539562800067 1
## 21 CRIX.UPBIT.KRW-BTC 2018-10-15T00:20:00+00:00 1539562858528 1
## 22 CRIX.UPBIT.KRW-BTC 2018-10-15T00:21:00+00:00 1539562905944 1
## 23 CRIX.UPBIT.KRW-BTC 2018-10-15T00:22:00+00:00 1539562980195 1
## 24 CRIX.UPBIT.KRW-BTC 2018-10-15T00:23:00+00:00 1539563035859 1
## 25 CRIX.UPBIT.KRW-BTC 2018-10-15T00:24:00+00:00 1539563100331 1
## 26 CRIX.UPBIT.KRW-BTC 2018-10-15T00:25:00+00:00 1539563159175 1
## 27 CRIX.UPBIT.KRW-BTC 2018-10-15T00:26:00+00:00 1539563219637 1
## 28 CRIX.UPBIT.KRW-BTC 2018-10-15T00:27:00+00:00 1539563279541 1
## 29 CRIX.UPBIT.KRW-BTC 2018-10-15T00:28:00+00:00 1539563335368 1
## 30 CRIX.UPBIT.KRW-BTC 2018-10-15T00:29:00+00:00 1539563396462 1
## 31 CRIX.UPBIT.KRW-BTC 2018-10-15T00:30:00+00:00 1539563458659 1
## 32 CRIX.UPBIT.KRW-BTC 2018-10-15T00:31:00+00:00 1539563519648 1
## 33 CRIX.UPBIT.KRW-BTC 2018-10-15T00:32:00+00:00 1539563579233 1
## 34 CRIX.UPBIT.KRW-BTC 2018-10-15T00:33:00+00:00 1539563627331 1
## 35 CRIX.UPBIT.KRW-BTC 2018-10-15T00:34:00+00:00 1539563693444 1
## 36 CRIX.UPBIT.KRW-BTC 2018-10-15T00:35:00+00:00 1539563756593 1
## 37 CRIX.UPBIT.KRW-BTC 2018-10-15T00:36:00+00:00 1539563819353 1
## 38 CRIX.UPBIT.KRW-BTC 2018-10-15T00:37:00+00:00 1539563879113 1
## 39 CRIX.UPBIT.KRW-BTC 2018-10-15T00:38:00+00:00 1539563939137 1
## 40 CRIX.UPBIT.KRW-BTC 2018-10-15T00:39:00+00:00 1539564000119 1
## 41 CRIX.UPBIT.KRW-BTC 2018-10-15T00:40:00+00:00 1539564056070 1
## 42 CRIX.UPBIT.KRW-BTC 2018-10-15T00:41:00+00:00 1539564120024 1
## 43 CRIX.UPBIT.KRW-BTC 2018-10-15T00:42:00+00:00 1539564177075 1
## 44 CRIX.UPBIT.KRW-BTC 2018-10-15T00:43:00+00:00 1539564229447 1
## 45 CRIX.UPBIT.KRW-BTC 2018-10-15T00:44:00+00:00 1539564295469 1
## 46 CRIX.UPBIT.KRW-BTC 2018-10-15T00:45:00+00:00 1539564359337 1
## 47 CRIX.UPBIT.KRW-BTC 2018-10-15T00:46:00+00:00 1539564419889 1
## 48 CRIX.UPBIT.KRW-BTC 2018-10-15T00:47:00+00:00 1539564477562 1
## 49 CRIX.UPBIT.KRW-BTC 2018-10-15T00:48:00+00:00 1539564530155 1
## 50 CRIX.UPBIT.KRW-BTC 2018-10-15T00:49:00+00:00 1539564596505 1
## candleDateTimeKst openingPrice highPrice lowPrice tradePrice
## 1 2018-10-15T09:00:00+09:00 7200000 7206000 7196000 7199000
## 2 2018-10-15T09:01:00+09:00 7196000 7208000 7194000 7195000
## 3 2018-10-15T09:02:00+09:00 7205000 7205000 7190000 7198000
## 4 2018-10-15T09:03:00+09:00 7197000 7208000 7182000 7188000
## 5 2018-10-15T09:04:00+09:00 7199000 7199000 7184000 7188000
## 6 2018-10-15T09:05:00+09:00 7186000 7196000 7182000 7194000
## 7 2018-10-15T09:06:00+09:00 7194000 7197000 7187000 7196000
## 8 2018-10-15T09:07:00+09:00 7196000 7197000 7186000 7191000
## 9 2018-10-15T09:08:00+09:00 7191000 7191000 7183000 7186000
## 10 2018-10-15T09:09:00+09:00 7185000 7191000 7185000 7189000
## 11 2018-10-15T09:10:00+09:00 7189000 7190000 7183000 7189000
## 12 2018-10-15T09:11:00+09:00 7189000 7189000 7182000 7189000
## 13 2018-10-15T09:12:00+09:00 7189000 7189000 7186000 7187000
## 14 2018-10-15T09:13:00+09:00 7188000 7188000 7187000 7188000
## 15 2018-10-15T09:14:00+09:00 7188000 7191000 7186000 7190000
## 16 2018-10-15T09:15:00+09:00 7186000 7190000 7183000 7183000
## 17 2018-10-15T09:16:00+09:00 7188000 7189000 7180000 7182000
## 18 2018-10-15T09:17:00+09:00 7182000 7186000 7177000 7177000
## 19 2018-10-15T09:18:00+09:00 7177000 7177000 7170000 7171000
## 20 2018-10-15T09:19:00+09:00 7170000 7178000 7165000 7166000
## 21 2018-10-15T09:20:00+09:00 7166000 7176000 7166000 7171000
## 22 2018-10-15T09:21:00+09:00 7176000 7180000 7166000 7173000
## 23 2018-10-15T09:22:00+09:00 7171000 7175000 7166000 7172000
## 24 2018-10-15T09:23:00+09:00 7172000 7173000 7166000 7171000
## 25 2018-10-15T09:24:00+09:00 7171000 7171000 7165000 7170000
## 26 2018-10-15T09:25:00+09:00 7170000 7178000 7165000 7177000
## 27 2018-10-15T09:26:00+09:00 7175000 7175000 7173000 7173000
## 28 2018-10-15T09:27:00+09:00 7173000 7177000 7173000 7173000
## 29 2018-10-15T09:28:00+09:00 7177000 7177000 7173000 7174000
## 30 2018-10-15T09:29:00+09:00 7174000 7185000 7174000 7183000
## 31 2018-10-15T09:30:00+09:00 7182000 7182000 7175000 7180000
## 32 2018-10-15T09:31:00+09:00 7176000 7184000 7175000 7175000
## 33 2018-10-15T09:32:00+09:00 7175000 7177000 7173000 7177000
## 34 2018-10-15T09:33:00+09:00 7177000 7180000 7174000 7176000
## 35 2018-10-15T09:34:00+09:00 7176000 7180000 7176000 7177000
## 36 2018-10-15T09:35:00+09:00 7176000 7180000 7176000 7176000
## 37 2018-10-15T09:36:00+09:00 7177000 7180000 7177000 7179000
## 38 2018-10-15T09:37:00+09:00 7178000 7179000 7176000 7179000
## 39 2018-10-15T09:38:00+09:00 7179000 7180000 7178000 7179000
## 40 2018-10-15T09:39:00+09:00 7180000 7180000 7179000 7179000
## 41 2018-10-15T09:40:00+09:00 7180000 7188000 7179000 7188000
## 42 2018-10-15T09:41:00+09:00 7187000 7189000 7185000 7189000
## 43 2018-10-15T09:42:00+09:00 7189000 7190000 7188000 7190000
## 44 2018-10-15T09:43:00+09:00 7190000 7196000 7190000 7193000
## 45 2018-10-15T09:44:00+09:00 7193000 7193000 7190000 7190000
## 46 2018-10-15T09:45:00+09:00 7190000 7192000 7189000 7192000
## 47 2018-10-15T09:46:00+09:00 7192000 7193000 7188000 7193000
## 48 2018-10-15T09:47:00+09:00 7193000 7193000 7192000 7193000
## 49 2018-10-15T09:48:00+09:00 7192000 7193000 7192000 7193000
## 50 2018-10-15T09:49:00+09:00 7196000 7198000 7194000 7198000
## candleAccTradeVolume candleAccTradePrice candleDT candleDateTime2
## 1 4.08820 29435716 1 2018-10-15 00:00:00
## 2 12.76563 91915135 2 2018-10-15 00:01:00
## 3 3.61960 26056186 3 2018-10-15 00:02:00
## 4 15.21754 109340319 4 2018-10-15 00:03:00
## 5 6.54047 47043367 5 2018-10-15 00:04:00
## 6 3.42169 24592130 6 2018-10-15 00:05:00
## 7 5.11914 36823940 7 2018-10-15 00:06:00
## 8 4.04112 29065767 8 2018-10-15 00:07:00
## 9 7.59268 54564723 9 2018-10-15 00:08:00
## 10 9.86326 70883754 10 2018-10-15 00:09:00
## 11 8.34267 59951263 11 2018-10-15 00:10:00
## 12 4.07566 29296750 12 2018-10-15 00:11:00
## 13 1.40808 10121691 13 2018-10-15 00:12:00
## 14 1.64379 11815174 14 2018-10-15 00:13:00
## 15 3.26746 23486997 15 2018-10-15 00:14:00
## 16 8.67805 62379519 16 2018-10-15 00:15:00
## 17 10.58356 76030757 17 2018-10-15 00:16:00
## 18 8.54061 61319474 18 2018-10-15 00:17:00
## 19 16.22891 116383914 19 2018-10-15 00:18:00
## 20 9.64593 69141521 20 2018-10-15 00:19:00
## 21 9.14417 65539523 21 2018-10-15 00:20:00
## 22 4.68190 33563639 22 2018-10-15 00:21:00
## 23 15.18292 108832028 23 2018-10-15 00:22:00
## 24 1.20871 8664980 24 2018-10-15 00:23:00
## 25 3.57084 25596052 25 2018-10-15 00:24:00
## 26 3.39816 24367487 26 2018-10-15 00:25:00
## 27 3.69382 26497859 27 2018-10-15 00:26:00
## 28 9.36124 67159118 28 2018-10-15 00:27:00
## 29 3.53500 25363071 29 2018-10-15 00:28:00
## 30 9.04297 64917901 30 2018-10-15 00:29:00
## 31 2.98621 21438224 31 2018-10-15 00:30:00
## 32 4.44016 31865925 32 2018-10-15 00:31:00
## 33 4.54944 32642236 33 2018-10-15 00:32:00
## 34 1.40096 10054148 34 2018-10-15 00:33:00
## 35 1.57051 11274293 35 2018-10-15 00:34:00
## 36 5.63141 40430134 36 2018-10-15 00:35:00
## 37 1.21996 8758984 37 2018-10-15 00:36:00
## 38 2.92813 21018153 38 2018-10-15 00:37:00
## 39 3.30881 23754998 39 2018-10-15 00:38:00
## 40 1.57577 11313468 40 2018-10-15 00:39:00
## 41 2.94147 21124662 41 2018-10-15 00:40:00
## 42 2.87587 20672782 42 2018-10-15 00:41:00
## 43 4.45415 32024013 43 2018-10-15 00:42:00
## 44 0.84128 6049541 44 2018-10-15 00:43:00
## 45 6.62370 47638118 45 2018-10-15 00:44:00
## 46 1.25937 9055303 46 2018-10-15 00:45:00
## 47 3.01399 21675190 47 2018-10-15 00:46:00
## 48 3.25629 23422116 48 2018-10-15 00:47:00
## 49 2.65888 19124635 49 2018-10-15 00:48:00
## 50 1.42619 10264840 50 2018-10-15 00:49:00
4 분할 관련 코드
## Envelope
df_env<-as.data.frame(my.EnvelopeIndicator(res$openingPrice, m=20, p=0.02))
names(df_env)<-c("env_lower","env","env_upper")
res<-cbind(res, df_env)
kk<-ggplot(res)+
geom_line(aes(x=candleDateTime2, y=openingPrice), color="blue")+
geom_line(aes(x=candleDateTime2,y=env_lower), color="red")+
# geom_line(aes(x=candleDateTime2,y=env), color="black")+
geom_line(aes(x=candleDateTime2,y=env_upper), color="red")+
scale_x_datetime(labels = date_format("%Y-%m-%d"), breaks=pretty_breaks(n = 20)(as_datetime(c("2018-10-01", "2018-10-31"))))+
labs(x = "Date", y = "Envelope Price")+
theme(axis.text.x = element_text(angle = 40, hjust = 1))
## 볼린저 밴드
df_bbands<-as.data.frame(my.BBands(data=res, m=40, std=3))
res<-cbind(res,df_bbands)
bb<-ggplot(res)+
geom_line(aes(x=candleDateTime2, y=openingPrice), color="black")+
geom_line(aes(x=candleDateTime2,y=dn), color="red")+
geom_line(aes(x=candleDateTime2,y=mavg), color="blue")+
geom_line(aes(x=candleDateTime2,y=up), color="red")+
scale_x_datetime(labels = date_format("%Y-%m-%d"), breaks=pretty_breaks(n = 20)(as_datetime(c("2018-10-01", "2018-10-31"))))+
labs(x = "Date", y = "BBands Price")+
theme(axis.text.x = element_text(angle = 40, hjust = 1))
#set window
lay <- rbind(c(1,2),
c(3,3))
aa<-ggplot(res) + geom_line(aes(x=candleDateTime2, y=openingPrice), color="black")
grid.arrange(kk,bb,aa, layout_matrix = lay)
5 사용 코드 설명
아시다 시피 Envelope 그래프는, 볼린저 밴드 그래프 는 이전 포스팅에서 확인이 가능하십니다. (참고 : http://lareale.tistory.com/289)
이를 이용해 위처럼 3개의 화면 불할로 그래프를 나타내봤습니다. 15일 경의 상승 구간에 의해 sd=3 만큼의 변동이 크게 느껴지지 않네요. (BBands Price 참고)
이를 더 응용한다면 ggplot2 와 r shiny 를 이용해 원하는 지표를 적용한 그래프를 상단에 여러개를 뿌릴 수 있도록 하면 참고 지표로 좋을 거 같군요.
이상으로 ggplot2 의 화면 분할 포스팅을 여기서 마치겠습니다.
'정리 > 시각화' 카테고리의 다른 글
[IDE] VSCode Python Jupyter Notebook 설정 (Markdown부터) (2) | 2023.11.20 |
---|---|
[R] ggplot2 서로 다른 그래프 겹치기 (1) | 2023.11.14 |
[R] Rshiny highcharts로 주식 그래프 그리기 (그래프 배열) (1) | 2023.11.09 |
[R] ggplot에 내가 원하는 point 그리기 (0) | 2019.10.20 |
[R] 복수의 plot align 하기 정렬 하기 (0) | 2018.11.11 |