인터랙티브 그래프

-마우스 클릭에 반응하는 그래프

-주요 패키지 : plotly

install.packages("plotly")
library(plotly)

응용예시1 : 산점도

1. 산점도 그리기
ggplot(mpg, aes(x=displ, y=hwy))+geom_point()

2. 구동방식(drv)별 색깔 다르게 설정
ggplot(mpg, aes(x=displ, y=hwy, col=drv))+geom_point()

3.인터렉티브화
p=ggplot(mpg, aes(x=displ, y=hwy, col=drv))+geom_point()
ggplotly(p)
#viewer에서 확인가능
#export>save as webpage 처리시 웹사이트로 확인가능

응용예시: 막대그래프

df=diamonds이

1. 표만들기
#cut 및 clarty 별 빈도
table(diamonds$cut)
table(diamonds$clarity)
df= diamonds %>%    
    group_by(cut, clarity) %>%
    summarise(n=n())

2.그래프그리기
ggplot(df, aes(x=cut, y=n)) + geom_col()
#색칠하기
p= ggplot(df, aes(x=cut, y=n, fill=clarity)) + geom_col(position="dodge")

3. 인터랙티브화하기
ggplotly(p)

+ Recent posts