Chelsey 2023. 5. 29. 17:28
728x90

<ggplot2 기본 구조>

1. Data

2. Mapping

ggplot(dataset) + geom f() +
	scale +

+ 를 이용해서 코드를 연결해 설정할 수 있다.

  • layer
    • 'geom_'로 시작하는 함수로 표현
    • geom_bar() 막대그래프 , geom_col() 
  • sacle
  • coord
  • facet
  • theme

 

layer

aes() 구문

swtwd("현재경로") 고정시킬 수 있다.

setwd("...")
load("sah.RData")
library(ggplot2)
ggplot(sah) + geom_point(aes(x=age, y=sbp))
ggplot(sah, aes(x=age, y=sbp)) + geom_point()

// 색상으로 관상동맥질환여부 표시하기
ggplot(sah, aes(age, sbp, color=chd)) + geom_point()

평활곡선 그래프

ggplot(sah, aes(age, sbp, color=chd)) + geom_smooth()

scale

보통 자동으로 지정된다.

 

add 절대비교, mult 상대비교

// 0 ~ 100 까지 
a + scle_x_continous(limits=c(0,100), expand=expansion(0))

// 0 ~ 110까지
a + scle_x_continous(limits=c(0,100), expand=expansion(add=10)) 

// 0 ~ 100 까지의 0.3배 -> -30 ~ 130 까지 표현
a + scle_x_continous(limits=c(0,100), expand=expansion(mult=0.3))

로그변환 취하기

ggplot(sah, aes(x=age, y=ldl)) + geom_point()
ggplot(sah, aes(x=age, y=log(ldl))) + geom_point()
ggplot(sah, aes(x=age, y=ldl)) + geom_point() +
	scle_y_continuous(trans="log")
728x90