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

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

library(showtext)
### 한글 폰트 설정 
font_add_google("Nanum Gothic", "nanumgothic")
## 데이터 불러오기 : 학력별 소득 데이터 
data <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/school_earnings.csv")
data %>% head()
##      School Women Men Gap
## 1       MIT    94 152  58
## 2  Stanford    96 151  55
## 3   Harvard   112 165  53
## 4    U.Penn    92 141  49
## 5 Princeton    90 137  47
## 6   Chicago    78 118  40

01. 기본 bubble chart

plot_ly(data=data, x=~Women, y=~Men,
        type='scatter',
        mode="markers",
        marker = list(size = ~Gap)) %>% 
  
  layout(title = "대학별 여성과 남성의 소득차이(Gap)",
         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))

02. Marker의 color 속성

1) 전체 버블 색상 바꾸기

plot_ly(data=data, x=~Women, y=~Men,
        type='scatter',
        mode="markers",
        marker = list(size = ~Gap, 
                      color = "#F6C6BD")) %>% 
  
  layout(title = "대학별 여성과 남성의 소득차이(Gap)",
         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=data, x=~Women, y=~Men,
        type='scatter',
        mode="markers",
        marker = list(size = ~Gap, 
                      color = "#F6C6BD",
                      line = list(color = "#F6C6BD"))) %>% 
  
  layout(title = "대학별 여성과 남성의 소득차이(Gap)",
         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) 연속형 변수를 color 속성에 매핑하기

plot_ly(data=data, x=~Women, y=~Men,
        color = ~Gap, colors = 'Reds',
        type='scatter',
        mode="markers",
        marker = list(size = ~Gap)) %>% 
  
  layout(title = "대학별 여성과 남성의 소득차이(Gap)",
         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))

3) 범주형 변수를 color 속성에 매핑하기

data <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/school_earnings.csv")

## 범주형 변수 추가
data$State <- as.factor(c('Massachusetts', 'California', 'Massachusetts', 'Pennsylvania', 'New Jersey', 'Illinois', 'Washington DC',
                          'Massachusetts', 'Connecticut', 'New York', 'North Carolina', 'New Hampshire', 'New York', 'Indiana',
                          'New York', 'Michigan', 'Rhode Island', 'California', 'Georgia', 'California', 'California'))


plot_ly(data, x = ~Women, y = ~Men, text = ~Gap, 
        type = 'scatter', mode = 'markers',
        size = ~Gap,
        color = ~State, # 범주형 변수 
        colors = 'Paired',
        marker = list(opacity = 0.5, sizemode = 'diameter')) %>% 
  
  layout(title = "대학별 여성과 남성의 소득차이(Gap)",
         font=list(family ='nanumgothic'),
         xaxis = list(title="", zeroline = F), 
         yaxis = list(title="", zeroline = F), 
         margin = list(l=10, r=10, b=0, t=30, pad=0))

03. 버블의 size 스케일 조정

plot_ly(data, x = ~Women, y = ~Men, text = ~Gap,
        color = ~State, colors = 'Paired', 
        size = ~Gap, 
        sizes =c(1,100),
        type='scatter',
        mode="markers",
        marker = list(opacity = 0.5, sizemode ='diameter')) %>% 
  
  layout(title = "대학별 여성과 남성의 소득차이(Gap)",
         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))