이번 chpt에서는 ggplot의 여러 가지 geom functions에 대해서 알아보자.
library(ggplot2)
df <- data.frame(
x = c(3, 1, 5),
y = c(2, 4, 6),
label = c("a","b","c")
)
head(df)
실습을 위해 우선 df를 만들어주고 워밍업으로 geom_point 함수를 복습차원에서 사용해 보겠다.
ggplot(df, aes(x,y) +
geom_point()
1. geom_text()
geom_text() 함수의 aesthetic에 label option을 사용하여 점이 아닌 text로 시각화가 가능하다.
ggplot(df, aes(x,y) +
geom_text(aes(label=label))
2. geom_line & path
ggplot(df, aes(x,y)) +
geom_line()
전 챕터에서 배운 내용대로 geom_line함수는 점을 x축변수를 기준으로 잇는다. path와 비교해 보자.
ggplot(df, aes(x,y)) +
geom_path()
3. geom_area()
ggplot(df, aes(x,y)) +
geom_area()
geom_area()는 geom_line에서 x축 방향의 모든 부분을 색을 칠해준다. alpha옵션을 사용하여 재밌는 시각화도 가능하다.
ggplot(df, aes(x,y)) +
geom_area(fill = 'yellow', alpha=0.2)
4. geom_polygon
ggplot(df, aes(x=x, y=y)) +
geom_polygon()
geom_polygon은 area와 비슷하지만 점들 사이의 area를 색칠해 준다.
5. geom_tile()
ggplot(df, aes(x,y)) +
geom_tile()
geom_tile의 경우는 이후에 hexbin, bin2d, raster 그래프와 같이 3차원의 시각화가 가능한 그래프이기 때문에 다방면의 쓰임새를 가질 수 있다. 지금은 간단한 데이터로 시각화를 했기에 그저 그런 모양을 가지고 있지만 피쳐가 다양해진다면 그럴싸한 모양을 갖출 수 있는 그래프가 된다.
'R > Exploratory data analysis' 카테고리의 다른 글
R for Data Science::scales 2 (0) | 2023.05.24 |
---|---|
ggplot2::Position scales and axes (0) | 2023.05.21 |
ggplot2:: geom_graphs (0) | 2023.04.24 |
ggplot2:: geom_smooth() (0) | 2023.04.06 |
ggplot2:: Faceting (0) | 2023.04.06 |