R 패키지/ggplot2

[R 통계_ggplot2 패키지] 09. 색상을 조정하는 방법들

2021. 10. 21. 23:13
728x90

지난번에 살펴본 geom_bar Plot을 기준으로, ggplot 내의 색상을 넣는 다양한 방법에 대해 알아보려 한다.

 

11-1. ggplot 색 적용 방식 1 (Default 색상명 활용)

그래프1 <- ggplot(데이터명, aes(x = x축))

그래프1 <- geom_bar(fill = "색상명")

#11-1. ggplot 색 적용 방식 1 (Default 색상명 활용)
red <- ggplot(mtcars, aes(x = factor(cyl)))+
geom_bar(fill = "red")
red

기본 색상명("red")를 활용한 색상 넣기

 

11-2.  ggplot 색 적용 방식 2 (HEX 컬러 코드 활용)

그래프1 <- ggplot(데이터명, aes(x = x축))

그래프1 <- geom_bar(fill = "HEX 컬러코드")

yellow <- ggplot(mtcars, aes(x = factor(cyl)))+
  geom_bar(fill = "#edae49")
yellow

HEX 컬러코드 "#edae49"를 활용하여 노르스름 한 색 입히기

 

11-3.  ggplot 색 적용 방식 3 (Manual 색상 선택)

그래프1 <- ggplot(데이터명, aes(x = x축))

그래프1 <- geom_bar()

scale_fill_manual(values = c("컬러명1", "컬러명2", "컬러명3"))  * 테두리 색의 경우 scale_color_manual로 통제 가능하다

colorchip <- c("#d1495b", "#edae49", "#66a182")

colorful <- ggplot(mtcars, aes(x = factor(cyl), fill = factor(cyl)))+
geom_bar()+
scale_fill_manual(values=colorchip)

Manual 컬러 선택을 통한 bar Chart

 

11-4.  ggplot 색 적용 방식 4 (미리 정제 된 색상 선택)

그래프1 <- ggplot(데이터명, aes(x = x축))

그래프1 <- geom_bar()

scale_fill_brewer(palette="정제된 색상조합명")

  * 테두리 색의 경우 scale_color_brewer로 통제 가능하다

#11-4. ggplot 색 적용 방식 4 (Brewer 된 색상 선택)
colorful2 <- ggplot(mtcars, aes(x = factor(cyl), fill = factor(cyl)))+
geom_bar()+
scale_fill_brewer(palette="RdPu")

colorful2

brewer된 색상칩을 활용한 Bar Chart

미리 brewer 된 색상 코드들은 다음과 같다.

brewer된 색상

 

 

 

 

<참고자료>

http://www.sthda.com/english/wiki/ggplot2-colors-how-to-change-colors-automatically-and-manually#change-colors-manually

 

ggplot2 colors : How to change colors automatically and manually? - Easy Guides - Wiki - STHDA

Statistical tools for data analysis and visualization

www.sthda.com

 

728x90