오늘 사용할 데이터는 palmerpenguin::penguins
library(palmerpenguins)
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.7
## v tidyr 1.2.0 v stringr 1.4.0
## v readr 2.1.2 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
시작 전 데이터 훑어보기
penguins %>% dlookr::diagnose()
## # A tibble: 8 x 6
## variables types missing_count missing_percent unique_count unique_rate
## <chr> <chr> <int> <dbl> <int> <dbl>
## 1 species fact~ 0 0 3 0.00872
## 2 island fact~ 0 0 3 0.00872
## 3 bill_length_mm nume~ 2 0.581 165 0.480
## 4 bill_depth_mm nume~ 2 0.581 81 0.235
## 5 flipper_length_mm inte~ 2 0.581 56 0.163
## 6 body_mass_g inte~ 2 0.581 95 0.276
## 7 sex fact~ 11 3.20 3 0.00872
## 8 year inte~ 0 0 3 0.00872
1. ggplot()만 켜보기
penguins %>% ggplot()

2. aes 설정하기
x, y
alpha(투명도)
color
fill
shape
size 등을 설정
p <- penguins %>% ggplot() +
aes(x = bill_length_mm,
y = bill_depth_mm)
p

3. geom_ : 어떤 기하학적 요소?
geom_point()
geom_bar()
geom_contour()
geom_path()
geom_ 안에 aes를 쓸 수 있음
p <- p +
geom_point(aes(color = as.factor(species)
, size = (body_mass_g / 1000)
, alpha = 0.7))
p
## Warning: Removed 2 rows containing missing values (geom_point).

4. scale layer
x축, y축 설정
p <- p +
scale_y_continuous(breaks = seq(0, 30, by = 2)
, labels = paste(seq(0, 30, by = 2), "mm") # y축 단위
, minor_breaks = NULL) + # 중간 중간 흰선 없애기
scale_x_continuous(breaks = seq(30, 60, by = 10)
, labels = paste(seq(30, 60, by = 10), "mm")
, minor_breaks = NULL)
p
## Warning: Removed 2 rows containing missing values (geom_point).

투명도, 사이즈, 색깔 설정
p <- p +
scale_alpha_identity() + #투명도를 '제대로' 반영해줘 - 그림(legend)에서 확인 가능
scale_size_identity() + #size를 '제대로' 반영해줘 - 그림(legend)에서 확인 가능
scale_color_brewer(
palette = "Set1",
labels = c("Adele 펭귄",
"Chinstrap 펭귄",
"Gentoo 펭귄"))
p
## Warning: Removed 2 rows containing missing values (geom_point).

5. 범례 설정
p <- p +
guides(color = guide_legend( #범례 모양
title = "펭귄종류",
ncol = 3)) +
theme(legend.position = "bottom")
p
## Warning: Removed 2 rows containing missing values (geom_point).

6. facet 레이어 설정
p <- p +
facet_wrap(vars(island))
p
## Warning: Removed 2 rows containing missing values (geom_point).

7. 제목, 부제목 설정
p <- p +
labs(title = "팔머펭귄 시각화",
subtitle = "종별 부리길이 vs 부리깊이",
caption = "idlhy idlhy idlhy",
x = "부리 길이 (단위:mm)",
y = "부리 깊이 (단위:mm)")
p
## Warning: Removed 2 rows containing missing values (geom_point).
