plotly in R
※ 데이터시각화 패키지: plotly
- 반응형 그래프
plot_ly
- 기본 구조
- plot_ly() : data와 x, y축 값을 넣어줌
- parameter
- type : plot 종류 ( “scatter” / “box” / “bar” / … )
- mode : “markers” / “line”
- color: 색깔 추가
# 기본형식
> plot_ly(data, x, y, type, mode, color, …)
# 사용예시
> df %>%
plot_ly(x = ~x인자, y = ~y인자, type = "" "", mode = "" "", color = ~컬럼명)"
□ Box plot (상자 그림)
# 기본형식
> plot_ly(data, x, y, type = "box")
# 사용예시
> plot_ly(data, x= ~"범주형변수", y = ~"target변수",
type = "box",
color = " ", # 색상 추가
boxmean = T, # 박스플롯에 평균선 추가
boxpoints = "all", # 박스플롯에 데이터포인트 추가
jitter = 숫자) # 데이터포인트 흩뿌리기
□ Line chart (선 그래프)
- parameter
- name : 범례
- add_trace() : 이미 만들어진 line chart에 추가로 그리고 싶을 때
- line : 선 커스텀 color / dash / width
- color : 색깔
- dash : “dash” / “dot” / …
- width : 굵기
- layout
- title / xaxis / yaxis
# 기본형식
> plot_ly(data, x, y, type = "scatter", mode = "lines")
# 사용예시
> plot_ly(data, x = ~"변수", y = ~"target변수", type = "scatter", mode = "lines",
name = "이름") %>% # 범례 추가
layout(title = "타이틀명", xaxis = "x축이름", yaxis = "y축이름") # 타이틀 / x,y축 이름 추가
# 변수별 라인 추가
> plot_ly(data, x = ~"변수", y = ~"target변수", type = "scatter", mode = "lines", name = "이름") %>%
add_trace(y = ~"변수1", mode = "lines", name = "이름1") %>%
add_trace(y = ~"변수2", mode = "lines", name = "이름2", line = list(color = " ", dash = "line", width = 숫자)) %>%
add_trace(y = ~"변수3", mode = "lines", name = "이름3", line = list(color = " ", dash = "dot", width = 숫자))
# 그래프 조절하기
> plot_ly(data, x = ~"변수", y = ~"target변수", type = "scatter", mode = "lines", name = "이름") %>%
layout(xaxis = list(rangeslider = list(type = ~"변수")))
layout(xaxis = list(rangeslider = list(type = ~"변수")))
□ Scatter plot (산점도)
- parameter
- marker 커스텀
- color : 색깔
- width : 굵기
- size : 점 크기
# 기본형식
> plot_ly(data, x, y, type = "scatter", mode = "markers")
# 사용예시
> plot_ly(data, x = ~"변수", y = ~"target변수", type = "scatter", mode = "scatter") %>%
marker = list(size = 숫자, color = '색깔', line = list(color = '색깔', width = 숫자)) # marker 커스텀
댓글남기기