[R 통계_ggplot2 패키지] 09. 색상을 조정하는 방법들
지난번에 살펴본 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
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
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)
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 된 색상 코드들은 다음과 같다.
<참고자료>
ggplot2 colors : How to change colors automatically and manually? - Easy Guides - Wiki - STHDA
Statistical tools for data analysis and visualization
www.sthda.com