데이터 시각화

RSTUDIO 에서 COLOR 선택하기(ggsci,tidyquant)

r-code-for-data-analysis 2023. 5. 29. 07:24

R로 데이터 전처리, 분석을 한 후 시각화를 할 때 가장 고민이 되는 것은 점, 선의 색깔을 어떻게 해야 할까이다.

 

이공대 출신인만큼 색에 대한 감각이 부족하므로...

 

최근에 발견한 패키지 중 "ggsci" 와 "tidyquant" 가 매우 유용해서 자주 사용한다.

 

https://github.com/nanxstats/ggsci

 

GitHub - nanxstats/ggsci: 🦄 Scientific journal and sci-fi themed color palettes for ggplot2

🦄 Scientific journal and sci-fi themed color palettes for ggplot2 - GitHub - nanxstats/ggsci: 🦄 Scientific journal and sci-fi themed color palettes for ggplot2

github.com

https://github.com/business-science/tidyquant

 

GitHub - business-science/tidyquant: Bringing financial analysis to the tidyverse

Bringing financial analysis to the tidyverse. Contribute to business-science/tidyquant development by creating an account on GitHub.

github.com

 

[tidyquant] 사용법

1. 패키지 설치 및 불러오기

 

install.packages("tidyquant")
library(tidyquant)

 

2. 간단한 예제

 

Si 재료에 solvent 희석 비율에 따른 경도 데이터를 불러와서 boxplot을 그리되, 평균을 표시하는 내용

 

 

means <-df %>% fill(희석비율, .direction = "down") %>% pivot_longer(cols = `0mm`:`300mm`, names_to = "position", values_to = "Hardness") %>% na.omit() %>%
  group_by(희석비율) %>% summarise(avg = mean(Hardness))

df %>% fill(희석비율, .direction = "down") %>% pivot_longer(cols = `0mm`:`300mm`, names_to = "position", values_to = "Hardness") %>% na.omit() %>% 
  ggplot(aes(x=희석비율, y=Hardness , fill= 희석비율))+geom_boxplot()+
  labs(col = "희석비율(%)",
       title = "희석 비율에 따른  Si-pad  경도", 
       y="Shore 경도",
       x= "희석 비율(%)")+
  scale_fill_tq()+
  scale_color_tq()+
  theme_tq()+
  stat_summary(fun=mean, colour="darkred", geom="point", 
               shape=18, size=3, show.legend=FALSE) + 
  geom_text(data = means, aes(label = paste0("avg=",round(avg)), y = avg + 2))

 

3. 결과

 

 

4. 동일한 시각화를 ggsci 패키지를 이용하여 다시 그려봄.

 

다양한 테마가 많은데, 그 중에 Tron Legacy 를 자주 사용함.

 


df %>% fill(희석비율, .direction = "down") %>% pivot_longer(cols = `0mm`:`300mm`, names_to = "position", values_to = "Hardness") %>% na.omit() %>% 
  ggplot(aes(x=희석비율, y=Hardness , fill= 희석비율))+geom_boxplot()+
  labs(col = "희석비율(%)",
       title = "희석 비율에 따른  Si-pad  경도", 
       y="Shore 경도",
       x= "희석 비율(%)")+
  scale_color_tron()+
  scale_fill_tron()+
  theme_dark() + 
  theme(panel.background = element_rect(fill = "#2D2D2D") ) +
  stat_summary(fun=mean, colour="yellow", geom="point", 
               shape=18, size=3, show.legend=FALSE) + 
  geom_text(data = means, aes(label = paste0("avg=",round(avg)), y = avg + 2), col="yellow")

 

728x90
반응형