[R_bigdata] R을 활용한 시각화 (ggplot)

#day04_05_visual.R
#ggplot
install.packages('ggplot2')
library(ggplot2)
library(dplyr)
library(tidyverse)

#내장 데이터 사용
diamonds

#ggplot은 데이터와 미학적 요소로 나뉘어 그려진다.
#ggplot() 은 데이터를 정의
#aes(esthetics): 화면에 그려지는 방식
ggplot(diamonds,aes(x=color)) + geom_bar()

ggplot(diamonds,aes(x=clarity, y=price)) + geom_bar(stat='identity') #보이도록 하기 위해서 (visible)

#그룹화 해서 평균 가격을 만들어서 시각화
diamonds %>% group_by(clarity) %>% summarise(mean_price=mean(price)) %>% ggplot(aes(x=clarity,y=mean_price)) + geom_bar(stat='identity')
#group_by는 매개변수에 전달된  컬럼명으로 요약하는 역할을 한다. summarise()는 매개변수에 전달된 계산방법에 따라 group_by()의 컬럼그룹을 요약한다. 여기서는 clarity로 카테고리화하고 카테고리별 price를 mean()을 통해 구해내고 있다.

#산점도를 이용, 다이아몬드 크기에 따른 가격을 그려보기
diamonds %>% ggplot(aes(x=carat, y =price, color=clarity))+geom_point()+geom_smooth() #geom_point : 산점도

#ggplot 의 상속
#ggplot에서 정의한 내용은 전체에 상속된다.
#추가되는 레이어에서 정의 내용은 상속되지 않는다.
diamonds %>% ggplot(aes(x=carat, y =price))+geom_point(aes(color=clarity))+geom_smooth()

댓글