-
Notifications
You must be signed in to change notification settings - Fork 0
/
_common.R
171 lines (136 loc) · 5.72 KB
/
_common.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
library(tidyverse)
library(openintro)
# k_penguins 데이텃 ----------------------------------------------------------
k_penguins <- palmerpenguins::penguins %>%
# 영어 변수명 한글 변환
set_names(c("종명칭", "섬이름", "부리_길이", "부리_깊이", "물갈퀴_길이",
"체중", "성별", "연도")) %>%
# 영어 값 한글 값으로 변환
mutate(성별 = ifelse(성별 == "male", "수컷", "암컷"),
섬이름 = case_when( str_detect(섬이름, "Biscoe") ~ "비스코",
str_detect(섬이름, "Dream") ~ "드림",
str_detect(섬이름, "Torgersen") ~ "토르거센"),
종명칭 = case_when( str_detect(종명칭, "Adelie") ~ "아델리",
str_detect(종명칭, "Chinstrap") ~ "턱끈",
str_detect(종명칭, "Gentoo") ~ "젠투")
) %>%
# 자료형 변환
mutate(성별 = factor(성별, levels = c("수컷", "암컷")),
섬이름 = factor(섬이름, levels = c("비스코", "드림", "토르거센")),
종명칭 = factor(종명칭, levels = c("아델리", "턱끈", "젠투")))
k_penguins_tbl <- k_penguins |>
drop_na()
# knitr chunk options ----------------------------------------------------------
if (knitr::is_html_output()) {
knitr::opts_chunk$set(out.width = "90%")
} else if (knitr::is_latex_output()) {
knitr::opts_chunk$set(out.width = "80%")
}
# knit options -----------------------------------------------------------------
options(knitr.kable.NA = "")
# kableExtra options -----------------------------------------------------------
options(kableExtra.html.bsTable = TRUE)
# dplyr options ----------------------------------------------------------------
options(dplyr.print_min = 6, dplyr.print_max = 6)
# ggplot2 theme and colors -----------------------------------------------------
if (knitr::is_html_output()) {
ggplot2::theme_set(ggplot2::theme_minimal(base_size = 13))
} else if (knitr::is_latex_output()) {
ggplot2::theme_set(ggplot2::theme_minimal(base_size = 11))
}
### 웨스 앤더슨
color_palette[1] <- wesanderson::wes_palette("Darjeeling1", n = 5)
ggplot2::update_geom_defaults("point", list(color = color_palette[1],
fill = color_palette[1]))
ggplot2::update_geom_defaults("bar", list(fill = color_palette[1],
color = "#FFFFFF"))
ggplot2::update_geom_defaults("col", list(fill = color_palette[1],
color = "#FFFFFF"))
ggplot2::update_geom_defaults("boxplot", list(color = color_palette[1]))
ggplot2::update_geom_defaults("density", list(color = color_palette[1]))
ggplot2::update_geom_defaults("line", list(color = color_palette[2]))
ggplot2::update_geom_defaults("smooth", list(color = color_palette[2]))
ggplot2::update_geom_defaults("dotplot", list(color = color_palette[1],
fill = color_palette[1]))
suppressWarnings(suppressMessages({
extrafont::loadfonts()
# 1. ggplot 그래프 ------------------------
extrafont::loadfonts("win")
## 테마 (글꼴) -----------------------------
gg_theme_korean <- function() {
# ggthemes::theme_tufte() +
ggplot2::theme_minimal() +
ggplot2::theme(
plot.title = element_text(family = "NanumSquare", size = 18, face = "bold"),
plot.subtitle = element_text(family = "MaruBuri", size = 13),
axis.title.x = element_text(family = "MaruBuri"),
axis.title.y = element_text(family = "MaruBuri"),
axis.text.x = element_text(family = "MaruBuri", size = 11),
axis.text.y = element_text(family = "MaruBuri", size = 11),
legend.title = element_text(family = "MaruBuri", size=13),
plot.caption = element_text(family = "NanumSquare", color = "gray20")
)
}
ggplot2::theme_set(gg_theme_korean())
}))
# gt theme and colors -----------------------------------------------------
suppressWarnings(suppressMessages({
# 2. gt 표 ------------------------
library(gt)
library(gtExtras)
gt_theme_korean <- function(gt_tbl) {
# Grab number of rows of data from gt object
n_rows <- nrow(gt_tbl$`_data`)
gt_tbl |>
gt_theme_538() |>
tab_options(
# column_labels.background.color = '#1E61B0', # R logo 파란색
table.font.names ="NanumSquare",
heading.title.font.size = px(26),
heading.subtitle.font.size = px(16),
heading.background.color = "transparent",
column_labels.font.weight = 'bold',
table_body.hlines.width = px(0),
data_row.padding = px(6),
heading.align = 'center',
stub.background.color = "#ffffff",
stub.font.weight = "bold",
source_notes.font.size = px(10),
row.striping.include_table_body = FALSE
) |>
cols_align( align = "center", columns = where(is.numeric)) |>
cols_align( align = "auto", columns = where(is.character)) |>
## 글꼴 달리 적용
tab_style(
style = cell_text(
font = "MaruBuri",
weight = 'bold'
),
locations = cells_title(groups = 'subtitle')
) |>
tab_style(
style = cell_text(
font = "MaruBuri",
weight = 'bold'
),
locations = cells_body()
) |>
tab_style(
style = cell_text(
font = "MaruBuri",
weight = 'bold'
),
locations = cells_column_labels()
) |>
tab_style(
style = cell_text(
font = "MaruBuri",
),
locations = cells_source_notes()
) |>
tab_style(
style = cell_fill(color = 'grey90'),
locations = cells_body(rows = seq(1, n_rows, 2))
)
}
}))