pl_part2_ch3

ch3. Trace

데이터 불러오기

df_취업률_500 <- readRDS("df_취업률_500.rds")
df_취업률 <- readRDS( "df_취업률.rds")
df_covid19 <- readRDS("df_covid19.rds")
df_covid19_100 <- readRDS("df_covid19_100.rds")
df_covid19_100_wide <- readRDS("df_covid19_100_wide.rds")
df_covid19_stat <- readRDS("df_covid19_stat.rds")
library(plotly) 
Loading required package: ggplot2

Attaching package: 'plotly'
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
df_취업률_500 |>
  filter(졸업자수 < 500) |>
  plot_ly() |> ## Plotly 초기화
  ## scatter 트레이스에 makers 모드 설정
  add_trace(type = 'scatter', mode = 'markers',
            x = ~졸업자수, y = ~취업자수, name = ~대계열, ## name 속성 설정
            ## marker 사이즈와 색상 설정
            marker = list(size = 3, color = 'darkblue')) ##add_trace에는 반드시 type 지정

Opacity, alpha

df_취업률_500 |> plot_ly() |>
  add_trace(type = 'scatter', mode = 'markers',
            x = ~졸업자수, y = ~취업자수,
            ## marker 내부에서 opacity 설정
            marker = list(opacity = 0.3, color = 'darkblue'))
df_취업률_500 |> plot_ly() |>
  add_trace(type = 'scatter', mode = 'markers',
            x = ~졸업자수, y = ~취업자수,
            marker = list(color = 'darkblue'),
            ## marker 외부에서 opacity 설정
            opacity = 0.3)

Showlegend

df_취업률_500 |>
  plot_ly() |>
  add_trace(type = 'scatter', mode = 'markers',
            x = ~졸업자수, y = ~취업자수, name = ~대계열, ##name을 컬러 구분으로 인식 
            showlegend = FALSE) ## showlegend를 FALSE로 설정
df_취업률_500 |>
  plot_ly() |>
  add_trace(type = 'scatter', mode = 'markers',
            x = ~졸업자수, y = ~취업자수, name = ~대계열,
            showlegend = T) ## showlegend를 TRUE로 설정

text, textposition, texttemplate

## 긴 형태의 100일간 코로나19 데이터 중에
df_covid19_100 |>
  ## 국가명으로 그룹화
  group_by(location) |>
  ## 확진자수의 합계를 new_cases로 산출
  summarise(new_cases = sum(new_cases)) |>
  ## X축을 location, Y축과 text를 new_case로 매핑
  plot_ly() |>
  add_trace(type = 'bar', ## bar 트레이스 설정
            x = ~location, y = ~new_cases,
            text = ~new_cases) ## 텍스트 설정
df_covid19_100 |>
  group_by(location) |>
  summarise(new_cases = sum(new_cases)) |>
  plot_ly() |>
  add_trace(type = 'bar', x = ~location, y = ~new_cases, text = ~new_cases,
            ## textposition을 'inside'로 설정
            textposition = 'inside')
#######################################
df_covid19_100 |>
  group_by(location) |>
  summarise(new_cases = sum(new_cases)) |>
  plot_ly() |>
  add_trace(type = 'bar', x = ~location, y = ~new_cases, text = ~new_cases,
            ## textposition을 'outside'로 설정
            textposition = 'outside')
#######################################
df_covid19_100 |>
  group_by(location) |>
  summarise(new_cases = sum(new_cases)) |>
  plot_ly() |>
  add_trace(type = 'bar', x = ~location, y = ~new_cases, text = ~new_cases,
            ## textposition을 'auto'로 설정
            textposition = 'auto')
#######################################
df_covid19_100 |>
  group_by(location) |>
  summarise(new_cases = sum(new_cases)) |>
  plot_ly() |>
  add_trace(type = 'bar', x = ~location, y = ~new_cases, text = ~new_cases,
            ## textposition을 'none'으로 설정
            textposition = 'none')

texttemplete

df_covid19_100 |>
  group_by(location) |>
  summarise(new_cases = sum(new_cases)) |>
  plot_ly() |>
  add_trace(type = 'bar', x = ~location, y = ~new_cases, text = ~new_cases,
            textposition = 'inside',
            texttemplate = '확진자수:%{text:,}') ## texttemplate를 설정

hoverinfo, hovertext, hovertemplate

df_취업률_500 |>
  plot_ly() |>
  add_trace(type = 'scatter', mode = 'markers',
            x = ~졸업자수, y = ~취업자수,
            hoverinfo = 'y') ## hoverinfo 설정

hovertext

df_취업률_500 |>
  plot_ly() |>
  add_trace(type = 'scatter', mode = 'markers',
            x = ~졸업자수, y = ~취업자수,
            ## hovertext의 설정
            hovertext = ~paste0('중계열:', 중계열, '\n', '소계열:', 소계열))

hovertemplete

df_취업률_500 |>
  plot_ly() |>
  add_trace(type = 'scatter', mode = 'markers',
            x = ~졸업자수, y = ~취업자수, hovertext = ~대계열,
            ## hovertemplate의 설정
            hovertemplate = ' 졸업자:%{x}, 취업자:%{y}, 대계열:%{hovertext}')