AWS에서 구축한 Rstudio plot 한글 깨짐 해결 방안

미완성의 신
6 min readAug 23, 2019

--

AWS에서 Rstudio server을 구축 하고 잘 사용 하다가 어느순간 plot에 한글을 적용하려다 보니 깨지는 현상이 생긴다. 이를 해결 하기 위해 두달동안 여러 가지 방법을 시도해본 결과, 최종적으로 도움이 될 법한 내용을 소개 하고자 한다.

  1. AWS 서버 구축 후에 다음의 코드를 입력 했을때, 아래와 같이 깨짐 현상을 확인해보자.
plot(c(1:5), main="가나다")

2. 깨짐 현상을 확인 했으니, 이를 해결해보도록 하자. 우선 AWS 서버의 폰트를 먼저 확인 해야 한다. 리눅스 환경에서 fc-list 를 입력 해보자. 폰트의 리스트를 확인 하는 작업이다.

$ fc-list/usr/share/fonts/default/Type1/c059016l.pfb: Century Schoolbook L:style=Bold
/usr/share/fonts/default/Type1/c059033l.pfb: Century Schoolbook L:style=Italic
/usr/share/fonts/default/Type1/p052024l.pfb: URW Palladio L:style=Bold Italic
/usr/share/fonts/default/Type1/d050000l.pfb: Dingbats:style=Regular
(... 생략 ...)

이곳에 한글 폰트가 없다. 이제 이에 대해 한글 폰트를 설치 해주자. 또는, fc-list :lang=ko로 입력해도 한글 폰트가 존재 하는지 확인이 가능하다.

리눅스에 한글 폰트 설치 하기(나눔 폰트)

리눅스에 한글 폰트를 설치 해보자.

  1. 우선 폰트가 있는 디렉토리로 이동하자.
$ cd /usr/share/fonts

2. 나눔 폰트는 네이버에서 제공하는 폰트를 사용한다 다음 코드를 써보자.

$ wget http://cdn.naver.com/naver/NanumFont/fontfiles/NanumFont_TTF_ALL.zip

3. 압축 이 되어 있어서 NanumFont디렉토리에 압축을 풀어 주도록 하자.

$ unzip NanumFont_TTF_ALL.zip -d NanumFont

4. 설치 완료후 zip파일은 제거

$ rm -f NanumFont_TTF_ALL.zip

5. fc-list :lang=ko코드로 한글 폰트가 잘 설치 되어 있는지 확인

$ fc-list :lang=ko
/usr/share/fonts/NanumFont/NanumBrush.ttf: 나눔손글씨 붓,Nanum Brush Script:style=Regular
/usr/share/fonts/NanumFont/NanumGothic.ttf: 나눔고딕,NanumGothic:style=Regular
/usr/share/fonts/NanumFont/NanumPen.ttf: 나눔손글씨 펜,Nanum Pen Script:style=Regular
/usr/share/fonts/NanumFont/NanumMyeongjoBold.ttf: 나눔명조,NanumMyeongjo:style=Bold
/usr/share/fonts/NanumFont/NanumMyeongjoExtraBold.ttf: 나눔명조,NanumMyeongjo,NanumMyeongjoExtraBold,나눔명조 ExtraBold:style=ExtraBold,Regular
/usr/share/fonts/NanumFont/NanumGothicBold.ttf: 나눔고딕,NanumGothic:style=Bold
/usr/share/fonts/NanumFont/NanumGothicExtraBold.ttf: 나눔고딕,NanumGothic,NanumGothicExtraBold,나눔고딕 ExtraBold:style=Bold,ExtraBold,Regular
/usr/share/fonts/NanumFont/NanumMyeongjo.ttf: 나눔명조,NanumMyeongjo:style=Regular

이로써, 리눅스에 한글 폰트는 잘 설치 했다. 특히 AWS는 첫 서버 구축 할때 한글 폰트가 없으니 이는 인지 할 필요가 있어 보인다. 이제 Rstudio server에서 폰트를 가져와서 사용해보자.

Rstudio server 에 한글 폰트 추가 하기

Rstudio에 한글 폰트를 추가 하자. 폰트를 추가 하는데에는, font_add() 라는 함수를 사용 할껀데, 이는 sysfonts패키지가 있어야 한다. font_add함수의 기본 사용법은 font_add( “폰트명”,”폰트 경로”) 이다. 여기서 폰트 경로는 리눅스에서 확인이 가능하다.

Ex) fc-list :lang=ko시 나오는 폰트 경로 예시

/usr/share/fonts/NanumFont/NanumGothicExtraBold.ttf: 나눔고딕,NanumGothic,NanumGothicExtraBold,나눔고딕 ExtraBold:style=Bold,ExtraBold,Regular
/usr/share/fonts/NanumFont/NanumMyeongjo.ttf: 나눔명조,NanumMyeongjo:style=Regular

이제 R에서 font_add 를 사용해보자.

library(sysfonts) # 없다면 install.packages("sysfonts")font_add("NanumGothicBold","/usr/share/fonts/NanumFont/NanumGothicBold.ttf")

그리고 나서 다시 아까 깨졌던 코드를 사용해보도록 하자.

plot(c(1:5), main="가나다")

이렇게 plot 에서 깨졌던 한글이 제대로 나오는 것을 알 수 있다.

총평

이 문제를 가지고 약 두달간 고생을 했다. 억지로 한글 폰트를 설치 해보려 블로그도 찾아보고, 영어로 된 R 버전을 한글로 바꾸기도 해보았다. 결국 AWS 기본 서버 환경에서의 한글 폰트 문제로 밝혀지긴 했지만 미리 알았다면 하는 생각이 든다. 두달이 지난 시점에서 지금은 조금이나마 AWS 의 환경에 더 알아 갔으니 좋은 댓가를 치뤘다고 생각하자.

--

--

미완성의 신

우리 인간은 미완성의 신들이다. 뛰어난 데이터 과학자를 꿈꾸는. 책을 좋아하는. 그런 사람입니다.