데이터 시각화

R을 이용한 논문용 그래프 (흑백용 패턴 색칠)

r-code-for-data-analysis 2024. 1. 21. 22:35

요즘엔 온라인으로 논문을 볼 수 있으므로 그래프에 색을 넣어서 사용할 수 있다. 
그러나 흑백으로 여전히 보는 사람이 있으므로 색깔만으로 구분하기 어려울 수 있다. 
 
그래서 흑백 패턴으로 그래프 색을 구별하는 그래프 그리는 패키지를 찾아 보았다. 
 
https://coolbutuseless.github.io/package/ggpattern/articles/geom-gallery-geometry.html#b-w-example
 

Geom Gallery - Geometry-based Patterns

ggpattern

coolbutuseless.github.io

 
 
예를 들어 아래와 같은 그래프를 넣는다고 해보자.

 
이걸 흑백으로 보는 사람은 아래와 같다. 온도별 그래프를 구별할 수 없을 것이다. 

 
 
그래서  ggpattern 패키지를 설치하고 그려보기로 했다. 

remotes::install_github("coolbutuseless/ggpattern")

library(ggpattern)

 
기존    geom_col(position = "dodge") 대신 아래와 같은 코드를 쓰면 아래와 같은 그래프를 얻을 수 있다. 
 

  geom_col_pattern( aes(pattern = Temperature, 
                        pattern_angle = Temperature, 
                        pattern_spacing = Temperature),
                    fill            = 'white',
                    colour          = 'black',
                    pattern_spacing = 0.02,
                    position = "dodge")+

 
mpg 데이터로 다시 한번 연습해보자

ggplot(mpg, aes(class)) +
  geom_bar_pattern(
    aes(
      pattern = class, 
      pattern_angle = class
    ), 
    fill            = 'white', 
    colour          = 'black',
    pattern_spacing = 0.025
  )+
  theme_bw(18) +
  theme(legend.position = 'none') +
  #coord_fixed(ratio = 1/15) +
  scale_pattern_discrete(guide = guide_legend(nrow = 1))+
  labs(title = "ggpattern::geom_bar_pattern()") +
  geom_text(stat ="count", 
            aes(label = ..count..),
            vjust =0, nudge_y = 1)

 

728x90
반응형