데이터 시각화

RSTUIDO에서 boxplot, density, point를 한번에 그리는 법(ggdist)

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

데이터 시각화 중 대표적인 것이 boxplot 이다. 그런데 개별점을 표시하는 point와 같이 쓸 경우 겹쳐 보여서 시인성이 안 좋다. 더구나 데이터 분포를 보는 density 까지 보여주려면 그래프가 지저분해진다.

 

이걸 해결해 주는 패키지가 "ggdist" 이다. 

 

https://mjskay.github.io/ggdist/

 

Visualizations of Distributions and Uncertainty

Provides primitives for visualizing distributions using ggplot2 that are particularly tuned for visualizing uncertainty in either a frequentist or Bayesian mode. Both analytical distributions (such as frequentist confidence distributions or Bayesian priors

mjskay.github.io

 

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

install.packages("ggdist")
library(ggdist)

 

2. 간단한 예제

library(tidyverse)
library(tidyquant)


mpg %>% 
  filter(cyl %in% c(4,6,8)) %>% 
  ggplot(aes(x=factor(cyl), y=hwy, fill=factor(cyl))) +
  
  ggdist::stat_halfeye(
    
    adjust = 0.5,
    justification = -.2,
    
    .width = 0,
    point_colour = NA
  ) +
  geom_boxplot(
    width = .12,
    outlier.color=NA,
    alpha=0.5
  )+
  ggdist::stat_dots(
    side="left",
    justification = 1.1,
    binwidth=.25
  ) +
  scale_fill_tq() +
  theme_tq()+
  labs(
    title = "Raincloud Plot",
    subtitle = "Showing the Bi-Modal Distribution of 6 Cylinder Vehicles",
    x="Engine Size (No.of Cylinder)",
    y="Highway Fuel Economy (MPG)",
    fill = "Cylinders"
  ) +
  coord_flip()

 

3. 결과 : 이때는 그래프를 가로로 뉘여서 보여주는게 유리하다. 3개의 그래프가 겹쳐 있으므로.

 

 

728x90
반응형