본문 바로가기

Lecture_Statistics

R로 데이터 시각화 _ ggplot2() _3

1. group 활용하기

library(nlme)
library(tidyverse)

head(Oxboys)
## Grouped Data: height ~ age | Subject
##   Subject     age height Occasion
## 1       1 -1.0000  140.5        1
## 2       1 -0.7479  143.4        2
## 3       1 -0.4630  144.8        3
## 4       1 -0.1643  147.1        4
## 5       1 -0.0027  147.7        5
## 6       1  0.2466  150.2        6

 

--> 각 age 별로 heigth가 변하는 정도를 알고 싶다  == subject 별로 age에 따른 height 변화를 알고 싶다. 

--> 이런 group의 기능과 유사한 기능을 제공하는 옵션이 바로 colour이다. 

ggplot(Oxboys, aes(age, height, group=Subject))+geom_line()+geom_point()
ggplot(Oxboys, aes(age, height, color=Subject))+geom_line()+geom_point()

 

# group + geom_line

group=  이라는 옵션을 ggplot에 설정할 때와 & 이후 geom에 설정할 때의 차이점

ggplot(Oxboys, aes(age, height, group=Subject))+geom_line()+geom_smooth(method='lm', se=FALSE)
ggplot(Oxboys, aes(age, height))+geom_line(aes(group=Subject))+geom_smooth(method='lm', size=2, se=FALSE)

 

 

# group + boxplot + geom_line

--> 상자 그림 위에 Subject별 profile line 그리기

ggplot(Oxboys, aes(Occasion, height))+geom_boxplot()+geom_line(aes(group=Subject), color='blue', alpha=0.5)

 

 

## Matching aesthetics to graphic objects

==> group=1 을 통해서 color= 라는 옵션으로 나눠줬던 group 정보를 무산시킨 것!

df <- data.frame(x=1:3, y=1:3, colour=c(1,3,5))
ggplot(df, aes(x,y, color=factor(colour)))+geom_line()+geom_point()
ggplot(df, aes(x,y, color=factor(colour)))+geom_line(aes(group=1), linewidth=2)+geom_point(size=5)
ggplot(df, aes(x,y, color=colour))+geom_line(aes(group=1), linewidth=2)+geom_point(size=5)


df1 <- data.frame(x=1:6, y=1:6, colour=c(1,1,3,3,5,5))
ggplot(df1, aes(x, y, colour=factor(colour)))+geom_line(linewidth=2)+geom_point(size=5)

 

 

 

2. fill 의 역할

## dodge  _ 쌓는게 아니라 옆으로 세워서 각 차이를 더 잘 보여준다고 생각한다

ggplot(mpg, aes(class))+geom_bar(aes(fill=drv))
ggplot(mpg, aes(class))+geom_bar(aes(fill=drv), position="dodge")

 

 

## theme를 활용한 x축의 label 조정

ggplot(mpg, aes(class, fill=drv))+geom_bar()+theme(axis.text.x=element_text(angle=45, hjust=1))

theme에서는 전체 타이틀 조절, x 및 y의 타이틀 조절 ratio legend 등등 다양한 내용을 조절

 

 

3. Surface plots

--> geom_tile( ) : data point가 중앙에 오는 tile을 그려준다

--> geom_raster(stat = 'identity') : tile의 크기가 모두 같게 조정한다 

--> geom_rect( ) : 사각형 그려주는 함수 aes(xmin xmax ymin ymax)

 

df <- data.frame(x=c(6,2,12), y=c(4,8,12), label=c("a", "b", "c"))

p <- ggplot(df, aes(x,y,label=label))+labs(x=NULL, y=NULL)



p+geom_tile()+geom_point(color='yellow', size=4)
p+geom_raster()+geom_point(color='yellow', size=4)

 

 

 

 

 

4. Contour plot  / colored tiles

--> geom_contour(aes( z = , colour = )

--> geom_raster(fill = )

 

## density의 높이마다 color를 다르게 하라는 명령어 ..level..  -> after_stat(level)로 업데이트 됨 

ggplot(faithfuld, aes(eruptions, waiting))+geom_contour(aes(z=density, colour=..level..))

ggplot(faithfuld, aes(eruptions, waiting))+geom_raster(aes(fill=density))

 

label <- data.frame(waiting=c(55,80), eruptions=c(2,4.3), label=c('peak one', "peak two"))

ggplot(faithfuld, aes(waiting, eruptions))+geom_raster(aes(fill=density))+geom_text(data=label, aes(label=label))

ggplot(faithfuld, aes(waiting, eruptions))+geom_raster(aes(fill=density))+geom_label(data=label, aes(label=label))

geom_text와 geom_label을 사용한 것의 차이점

 

 

5. Bubble plot  

--> geom_point 이용한 것!

small <-faithfuld[seq(1, nrow(faithfuld), by=10),]

ggplot(small, aes(eruptions, waiting))+geom_point(aes(size=density), alpha=1/3)

ggplot(small, aes(eruptions, waiting))+geom_point(aes(size=density), alpha=1/3)
+scale_size_area()

'Lecture_Statistics' 카테고리의 다른 글

R로 데이터 시각화 _ ggplot2() _2  (1) 2024.10.09
R로 데이터 시각화 _ ggplot2() _1  (1) 2024.10.07
R 프로그래밍 기본  (1) 2024.10.07