00.패키지 로드 및 데이터 불러오기

## 패키지 로드 
library(dplyr)
library(plotly)
library(RColorBrewer)

library(showtext)
### 한글 폰트 설정 
font_add_google("Nanum Gothic", "nanumgothic")
## 데이터 불러오기 : 크론병 데이터 
df <- read.csv("https://vincentarelbundock.github.io/Rdatasets/csv/robustbase/CrohnD.csv")
data.1 <- df[-1]
data.1 %>% head()
##      ID nrAdvE   BMI height country sex age weight   treat
## 1 19908      4 25.22    163      c1   F  47     67 placebo
## 2 19909      4 23.80    164      c1   F  53     64      d1
## 3 19910      1 23.05    164      c1   F  68     62 placebo
## 4 20908      1 25.71    165      c1   F  48     70      d2
## 5 20909      2 25.95    170      c1   F  67     75 placebo
## 6 20910      2 28.70    168      c1   F  54     81      d1

01.연속형 & 연속형 변수 산점도

plot_ly(data=data.1, x=~weight, y=~BMI,
        type='scatter',
        mode="markers") %>% 
  
  layout(title = "weight에 따른 BMI의 변화",
         font=list(family ='nanumgothic'),
         xaxis = list(title=""), 
         yaxis = list(title=""),
         margin = list(l=10, r=20, b=10, t=30, pad=0))

2) 회귀선 추가

## 산점도- 회귀선 추가
fig <- plot_ly(data=data.1, x=~weight, y=~BMI,
        type='scatter',
        mode="markers") %>% 
  layout(title = "weight에 따른 BMI의 변화",
         font=list(family ='nanumgothic'),
         xaxis = list(title=""), 
         yaxis = list(title=""),
         margin = list(l=10, r=20, b=10, t=30, pad=0))

## 회귀선 적합
fit.value <- lm(BMI~weight, data=data.1) %>% 
                  fitted.values() # y절편과 기울기
fig <- fig %>% 
        add_trace(x=~weight, y=~fit.value, mode='lines') %>% 
        layout(showlegend=F)
fig

02. 색상 및 투명도 지정

1) color 속성에 컬러코드 입력

plot_ly(data=data.1, x=~weight, y=~BMI,
        type='scatter',
        mode="markers",
        color = '#000080') %>% 
  
  layout(title = "weight에 따른 BMI의 변화",
         font=list(family ='nanumgothic'),
         xaxis = list(title=""), 
         yaxis = list(title=""),
         margin = list(l=10, r=20, b=10, t=30, pad=0))
## color = I('#000080')
plot_ly(data=data.1, x=~weight, y=~BMI,
        type='scatter',
        mode="markers",
        color = I('#000080')) %>% 
  
  layout(title = "weight에 따른 BMI의 변화",
         font=list(family ='nanumgothic'),
         xaxis = list(title=""), 
         yaxis = list(title=""),
         margin = list(l=10, r=20, b=10, t=30, pad=0))

2) color 속성에 범주형 매핑하기

plot_ly(data=data.1, x=~weight, y=~BMI,
        type='scatter',
        mode="markers",
        color = ~sex) %>% 
  
  layout(title = "weight에 따른 BMI의 변화",
         font=list(family ='nanumgothic'),
         xaxis = list(title=""), 
         yaxis = list(title=""),
         margin = list(l=10, r=20, b=10, t=30, pad=0))

3) plotly 컬러 팔레트 사용

plot_ly(data=data.1, x=~weight, y=~BMI,
        type='scatter',
        mode="markers",
        color = ~sex,
        colors = 'Set1') %>% # plotly 패키지 컬러 팔레트
  
  layout(title = "weight에 따른 BMI의 변화",
         font=list(family ='nanumgothic'),
         xaxis = list(title=""), 
         yaxis = list(title=""),
         margin = list(l=10, r=20, b=10, t=30, pad=0))

4) 색상 직접 지정

plot_ly(data=data.1, x=~weight, y=~BMI,
        type='scatter',
        mode="markers",
        color = ~sex,
        colors = c('#A0CFEC','#006A4E')) %>% 
  
  layout(title = "weight에 따른 BMI의 변화",
         font=list(family ='nanumgothic'),
         xaxis = list(title=""), 
         yaxis = list(title=""),
         margin = list(l=10, r=20, b=10, t=30, pad=0))

03.symbol 속성

1) 전체 심볼 모양 변경

plot_ly(data=iris, x = ~Sepal.Length, y = ~Petal.Length,
        type='scatter',
        mode="markers",
        color = ~Species,
        symbol = I('square-open')) %>% 
  
  layout(title = "symbol = 'square-open'",
         font=list(family ='nanumgothic'),
         xaxis = list(title=""), 
         yaxis = list(title=""),
         margin = list(l=10, r=20, b=10, t=30, pad=0))

2) symbol 속성에 범주형 변수 매핑하기

plot_ly(data=iris, x = ~Sepal.Length, y = ~Petal.Length,
        type='scatter',
        mode="markers",
        color = ~Species,
        symbol = ~ Species) %>% 
  
  layout(title = "symbol = ~ Species",
         font=list(family ='nanumgothic'),
         xaxis = list(title=""), 
         yaxis = list(title=""),
         margin = list(l=10, r=20, b=10, t=30, pad=0))
plot_ly(data=iris, x = ~Sepal.Length, y = ~Petal.Length,
        type='scatter',
        mode="markers",
        color = ~Species,
        symbol = ~ Species,
        symbols =c("circle-cross-open",
                   "star-diamond",
                   "pentagon")) %>% 
  
  layout(title = "Symbols",
         font=list(family ='nanumgothic'),
         xaxis = list(title=""), 
         yaxis = list(title=""),
         margin = list(l=10, r=20, b=10, t=30, pad=0))

04. 점의 투명도 조절

1) alpha 사용

plot_ly(data=diamonds, x = ~carat, y = ~price,
        type='scatter',
        mode="markers",
        color = ~carat,
        alpha =1) %>% 
  
  layout(title = "Alpha = 1",
         font=list(family ='nanumgothic'),
         xaxis = list(title="", zeroline =F), 
         yaxis = list(title="", zeroline =F), 
         margin = list(l=10, r=20, b=10, t=30, pad=0))
# plot_ly(data=diamonds, x = ~carat, y = ~price,
#         type='scatter',
#         mode="markers",
#         color = ~carat,
#         alpha =0.5) %>% 
#   
#   layout(title = "Alpha = 0.5",
#          font=list(family ='nanumgothic'),
#          xaxis = list(title="", zeroline =F), 
#          yaxis = list(title="", zeroline =F), 
#          margin = list(l=10, r=20, b=10, t=30, pad=0))
# 
# plot_ly(data=diamonds, x = ~carat, y = ~price,
#         type='scatter',
#         mode="markers",
#         color = ~carat,
#         alpha =0.25) %>% 
#   
#   layout(title = "Alpha = 0.25",
#          font=list(family ='nanumgothic'),
#          xaxis = list(title="", zeroline =F), 
#          yaxis = list(title="", zeroline =F), 
#          margin = list(l=10, r=20, b=10, t=30, pad=0))

2) opacity 사용

plot_ly(data=diamonds, x = ~carat, y = ~price,
        type='scatter',
        mode="markers",
        color = ~carat,
        opacity =1) %>% 
  
  layout(title = "opacity = 1",
         font=list(family ='nanumgothic'),
         xaxis = list(title="", zeroline =F), 
         yaxis = list(title="", zeroline =F), 
         margin = list(l=10, r=20, b=10, t=30, pad=0))
# plot_ly(data=diamonds, x = ~carat, y = ~price,
#         type='scatter',
#         mode="markers",
#         color = ~carat,
#         opacity =0.5) %>% 
#   
#   layout(title = "opacity = 0.5",
#          font=list(family ='nanumgothic'),
#          xaxis = list(title="", zeroline =F), 
#          yaxis = list(title="", zeroline =F), 
#          margin = list(l=10, r=20, b=10, t=30, pad=0))
# 
# plot_ly(data=diamonds, x = ~carat, y = ~price,
#         type='scatter',
#         mode="markers",
#         color = ~carat,
#         opacity =0.25) %>% 
#   
#   layout(title = "opacity = 0.25",
#          font=list(family ='nanumgothic'),
#          xaxis = list(title="", zeroline =F), 
#          yaxis = list(title="", zeroline =F), 
#          margin = list(l=10, r=20, b=10, t=30, pad=0))