The following object is masked from 'package:ggplot2':
last_plot
The following object is masked from 'package:stats':
filter
The following object is masked from 'package:graphics':
layout
margins_R <-list(t =50, b =25, l =25, r =25)
기본 구조
## 서브플롯 생성을 위한 기본 Plotly 객체 생성p_line_wide <- df_covid19_100_wide |>plot_ly()## 첫 번째 서브플롯 생성p1 <- p_line_wide |>add_trace(type ='scatter', mode ='lines', x =~date,y =~확진자_한국, name ='한국') |>layout(title ='한국',xaxis =list(tickfont =list(size =10)),yaxis =list(title =list(text ='확진자수')))## 두 번째 서브플롯 생성p2 <- p_line_wide |>add_trace(type ='scatter', mode ='lines', x =~date,y =~확진자_아시아, name ='아시아') |>layout(title ='아시아',xaxis =list(tickfont =list(size =10)),yaxis =list(title =list(text ='확진자수')))## 세 번째 서브플롯 생성p3 <- p_line_wide |>add_trace(type ='scatter', mode ='lines', x =~date,y =~확진자_유럽, name ='유럽') |>layout(title ='유럽',xaxis =list(tickfont =list(size =10)),yaxis =list(title =list(text ='확진자수')))## 네 번째 서브플롯 생성p4 <- p_line_wide |>add_trace(type ='scatter', mode ='lines', x =~date,y =~확진자_북미, name ='북미') |>layout(title ='북미',xaxis =list(tickfont =list(size =10)),yaxis =list(title =list(text ='확진자수')))## 다섯 번째 서브플롯 생성p5 <- p_line_wide |>add_trace(type ='scatter', mode ='lines', x =~date,y =~확진자_남미, name ='남미') |>layout(title ='남미',xaxis =list(tickfont =list(size =10)),yaxis =list(title =list(text ='확진자수')))## 여섯 번째 서브플롯 생성p6 <- p_line_wide |>add_trace(type ='scatter', mode ='lines', x =~date,y =~확진자_아프리카, name ='아프리카') |>layout(title ='아프리카',xaxis =list(tickfont =list(size =10)),yaxis =list(title =list(text ='확진자수')))## 일곱 번째 서브플롯 생성p7 <- p_line_wide |>add_trace(type ='scatter', mode ='lines', x =~date,y =~확진자_오세아니아, name ='오세아니아') |>layout(title ='오세아니아',xaxis =list(tickfont =list(size =10)),yaxis =list(title =list(text ='확진자수')))## 전체 서브플롯 구성subplots <-subplot(p1, p2, p3, p4, p5, p6, p7, nrows =3) |>layout( ## 전체 제목 설정title ='최근 100일간 코로나19 확진자수',## 전체 여백 설정margin = margins_R) ## margins_R <- list(t = 50, b = 25, l = 25, r = 25),ch2 chunk5subplots
group_by( )와 do( ) 사용 (서브플롯이 많을 때)
df_covid19_100 |>## 국가명으로 그룹화group_by(location) |>## 그룹화한 각각의 데이터 그룹들에 적용할 코드 설정do(## 각 그룹화한 데이터를 사용해 Plotly 객체 생성p =plot_ly(.) |>## line 모드의 scatter 트레이스 추가add_trace(type ='scatter', mode ='lines',## X, Y축에 변수 매핑, color를 설정x =~date, y =~new_cases, name =~location) |>add_annotations(x =0.5 , y =1.02, text =~location,showarrow = F, xref='paper',yref='paper', xanchor ='center') |>## layout으로 X, Y축을 설정layout(xaxis =list(tickfont =list(size =10)),yaxis =list(title =list(text ='확진자수'))) ) |>## 생성된 Plotly 객체들을 subplot 생성subplot(nrows =3, margin =0.04) |>## 생성된 subplot의 layout 설정layout(showlegend =TRUE,title ='최근 100일간 코로나19 확진자수',margin = margins_R) -> subplotssubplots ##x축 공간이 부족할 때 자동으로 30도 기울기 적용
Warning: No trace type specified and no positional attributes specified
No trace type specified:
Based on info supplied, a 'scatter' trace seems appropriate.
Read more about this trace type -> https://plotly.com/r/reference/#scatter
No scatter mode specifed:
Setting the mode to markers
Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
Warning: No trace type specified and no positional attributes specified
No trace type specified:
Based on info supplied, a 'scatter' trace seems appropriate.
Read more about this trace type -> https://plotly.com/r/reference/#scatter
No scatter mode specifed:
Setting the mode to markers
Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
1행에 크게 배치
subplot(## 아래의 nrows가 2이기 때문에 맨 위 열에 p1 하나를 위치시킴 p1,## subplot()으로 p2부터 p7까지를 묶어 하나의 플롯으로 만듬subplot(p2, p3, p4, p5, p6, p7,## 서브플롯은 2개의 열로 설정함으로써 2행 3열 서브플롯 생성nrows =2),## 전체 서브플롯은 2열로 구성nrows =2) |>## 범례 추가layout(showlegend =TRUE,## 전체 제목 설정title ='최근 100일간 코로나19 확진자수',## 전체 여백 설정margin = margins_R)