본문 바로가기
R

[R/R마크다운] 한글 폰트 적용하기 / extrafont

by ssanalyst 2020. 12. 12.
728x90

안녕하세요.

 

R마크다운을 작성하시면서 좀 더 이쁜 문서를 위해 폰트를 다양하게 적용하길 원하실겁니다.

 

그래서 이번 포스팅에서는 R마크다운에 폰트를 적용하는 방법에 대하 알아보겠습니다.

 

오늘 다룰 내용은 R마크다운만이 아니라 일반 R에도 적용가능하며 이렇게 적용한 폰트를 ggplot이나 base 그래프 제목이나 축에 폰트를 적용할 수 있습니다.

 

영어 폰트도 적용되니 이 방법 하나로 다 작업하실 수 있습니다.

 

R과 R마크다운에 원하는 폰트를 적용하게 도와주는 패키지 extrafont입니다.

 

extrafont 패키지를 사용하면 R에서도 컴퓨터에 설치된 폰트를 사용할 수 있습니다.

 

먼저 install.packages로 extrafont 패키지를 설치해줍니다.

 

내 R에 저장되어 있는 폰트(글씨체) 확인은 fontsfonttable을 사용하여 볼 수 있습니다.

 

fonts 함수는 간단하게 폰트 이름만 보여주고

 

fonttable 함수는 자세한 내용을 보여줍니다.

 

위 함수들로 어떤 폰트를 사용할 수 있나 확인해보시길 바랍니다.

 

 

저는 현재 8개의 폰트를 제 컴퓨터에서 R로 불러온 걸 보실 수 있습니다.

 

install.packages('extrafont')
library(extrafont)

fonts()
[1] "NanumGothic"                 "NanumGothicExtraBold"       
[3] "NanumGothic Light"           "NanumBarunGothic"           
[5] "NanumBarunGothic Light"      "NanumBarunGothic UltraLight"

fonttable()
  package                           afmfile                                           fontfile                    FullName                  FamilyName                   FontName  Bold Italic Symbol afmsymfile
1      NA                NanumGothic.afm.gz                C:\\Windows\\Fonts\\NanumGothic.ttf                 NanumGothic                 NanumGothic                NanumGothic FALSE  FALSE  FALSE         NA
2      NA            NanumGothicBold.afm.gz            C:\\Windows\\Fonts\\NanumGothicBold.ttf             NanumGothicBold                 NanumGothic            NanumGothicBold  TRUE  FALSE  FALSE         NA
3      NA       NanumGothicExtraBold.afm.gz       C:\\Windows\\Fonts\\NanumGothicExtraBold.ttf        NanumGothicExtraBold        NanumGothicExtraBold       NanumGothicExtraBold FALSE  FALSE  FALSE         NA
4      NA           NanumGothicLight.afm.gz           C:\\Windows\\Fonts\\NanumGothicLight.ttf           NanumGothic Light           NanumGothic Light           NanumGothicLight FALSE  FALSE  FALSE         NA
5      NA           NanumBarunGothic.afm.gz           C:\\Windows\\Fonts\\NanumBarunGothic.ttf            NanumBarunGothic            NanumBarunGothic           NanumBarunGothic FALSE  FALSE  FALSE         NA
6      NA       NanumBarunGothicBold.afm.gz       C:\\Windows\\Fonts\\NanumBarunGothicBold.ttf       NanumBarunGothic Bold            NanumBarunGothic       NanumBarunGothicBold  TRUE  FALSE  FALSE         NA
7      NA      NanumBarunGothicLight.afm.gz      C:\\Windows\\Fonts\\NanumBarunGothicLight.ttf      NanumBarunGothic Light      NanumBarunGothic Light      NanumBarunGothicLight FALSE  FALSE  FALSE         NA
8      NA NanumBarunGothicUltraLight.afm.gz C:\\Windows\\Fonts\\NanumBarunGothicUltraLight.ttf NanumBarunGothic UltraLight NanumBarunGothic UltraLight NanumBarunGothicUltraLight FALSE  FALSE  FALSE         NA

 

원하시는 폰트는 font_import 함수를 사용해서 불러오실 수 있습니다.

 

font_import()
font_import(pattern = "NanumGothic")

 

위 코드 첫 번째 줄의 font_import는 컴퓨터에 있는 모든 폰트를 불러오라는 뜻이고 컴퓨터의 모든 폰트를 불러오느라 시간이 조금 걸립니다.

 

그리고 두 번째 줄의 font_import 사용방식은 원하는 폰트만을 윈도우 폰트 폴더에서 찾아 불러옵니다.

 

괄호 안의 pattern = " " 의 " " 안에 원하시는 폰트 이름을 넣으시면 됩니다.

 

꼭 풀네임을 넣으실 필요는 없습니다.

 

 

나눔고딕을 예로 설명드리겠습니다.

 

나눔고딕은 폰트 family 이름이 ' NanumGothic'입니다.

 

여러분이 R에서 나눔고딕을 사용하고 싶어 font_import로 나눔고딕을 불러오려고 합니다.

 

그런데 컴퓨터에 나눔바른고딕, 나눔명조, 나눔스퀘어 같이 나눔 계열의 폰트가 여렇게 있다면

 

font_import(pattern = "NanumGothic") 이라고만 실행시키시셔도 위에 열거한 나눔 계열 폰트가 모두 검색되어 R에 등록됩니다.

 

그러니 원하는 폰트명을 정확하게 다 기재하실 필요는 없으십니다.

 

extrafont를 이용한 폰트 적용에 대해 알아보았습니다.

 

감사합니다.

 

 

728x90

댓글