Boxplot (박스플롯)
박스 플롯( Box Plot )은 데이터의 분포와 중심 경향을 그래프로 나타 낸 것이다.
박스 플롯은 데이터의 다섯 가지 요약 통계량(최솟값, 1사분위수, 중앙값, 3사분위수, 최댓값)을 시각적으로 나타내며 이상치(outliers)를 식별하는 데도 유용하다.
Rstudio 사분위수(Quartile) Outliers(이상치)
Rstudio 사분위수(Quartile) Outliers(이상치) IQR (사분범위) (tistory.com)
Rstudio 사분위수(Quartile) Outliers(이상치) IQR (사분범위)
Rstudio의 summary 함수를 통해서, 1사분위수(Q1), 2사분위수(Q2, 중앙값) 3사분위수(Q3)을 확인 summary (slam_dunk) uniform_no 4 : 1 5 : 1 6 : 1 7 : 1 8 : 1 9 : 1 (Other):6 name Length:12 Class :character Mode :character height Min. :16
logistician.tistory.com
R 기본함수 : boxplot 함수를 사용한 박스 플롯
library(palmerpenguins) data("penguins") #Dataset 불러오기 str(penguins) #Dataset 구성 확인 |
> str(penguins) #Dataset 구성 확인 tibble [344 × 8] (S3: tbl_df/tbl/data.frame) $ species : Factor w/ 3 levels "Adelie","Chinstrap",..: 1 1 1 1 1 1 1 1 1 1 ... $ island : Factor w/ 3 levels "Biscoe","Dream",..: 3 3 3 3 3 3 3 3 3 3 ... $ bill_length_mm : num [1:344] 39.1 39.5 40.3 NA 36.7 39.3 38.9 39.2 34.1 42 ... $ bill_depth_mm : num [1:344] 18.7 17.4 18 NA 19.3 20.6 17.8 19.6 18.1 20.2 ... $ flipper_length_mm: int [1:344] 181 186 195 NA 193 190 181 195 193 190 ... $ body_mass_g : int [1:344] 3750 3800 3250 NA 3450 3650 3625 4675 3475 4250 ... $ sex : Factor w/ 2 levels "female","male": 2 1 1 NA 1 2 1 2 NA NA ... $ year : int [1:344] 2007 2007 2007 2007 2007 2007 2007 2007 2007 2007 ... |
Box plot 그리기 #1
boxplot(penguins$body_mass_g, col = "darkblue",
main = "Body_mass",
ylab = "Body_mass(g)")
Box plot 그리기 #2 : Group (species)별 Box plot 표시
unique(penguins$species) |
> unique(penguins$species) [1] Adelie Gentoo Chinstrap Levels: Adelie Chinstrap Gentoo |
flipper_length_mm ~ species
# 그룹별로 색상 설정
group_colors <- c("Adelie" = "red", "Chinstrap" = "blue", "Gentoo" = "green")
# 박스 플롯 그리기
boxplot(flipper_length_mm ~ species, data = penguins,
main = "Flipper_length by species",
xlab = "Species", ylab = "Flipper_length(mm)",
col=group_colors)
이상치 포함 여부 : range = 0
range=0 : 이상치가 표현되지 않고, Whisker(수염)이 최대와 최소까지만 연결
# 별도로 데이터 저장 outer_eg <- penguins$body_mass_g summary(outer_eg) |
> summary(outer_eg) Min. 1st Qu. Median Mean 3rd Qu. Max. NA's 2700 3550 4050 4202 4750 6300 2 |
# Outliers (이상치) 데이터 추가 outer_eg[345:350] <- c(7000, 7800, 7600, 8000, 9100, 8100) summary(outer_eg) |
> summary(outer_eg) Min. 1st Qu. Median Mean 3rd Qu. Max. NA's 2700 3550 4050 4266 4812 9100 2 |
#range=0
boxplot(outer_eg, range = 0, col = "darkblue",
main = "Body_mass by specie (w/o outliers)",
xlab = "Species", ylab = "Body_mass(g)")
ggplot + geom_boxplot
geom_boxplot #1
library(tidyverse)
ggplot(data = penguins, aes(x = body_mass_g)) + geom_boxplot(fill="darkblue") +
labs(title = "Body_mass", x = "Body_mass(g)")
Warning message: Removed 2 rows containing non-finite values (`stat_boxplot()`). |
body_mass_g 데이터에 결측값이 포함됨에 따라, Warning message가 출력됨
table(is.na(penguins$body_mass_g)) |
FALSE TRUE 342 2 |
ggplot + geom_boxplot을 사용하는 경우는 그래프의 방향 (x축과 y축) 고려 필요
ggplot(data = penguins, aes(y = body_mass_g)) + geom_boxplot(fill="darkgreen") +
labs(title = "Body_mass", y = "Body_mass(g)")
geom_boxplot #2 : Group (species)별 Box plot 표시
x = species, col = species
ggplot(data = penguins, aes(x=species, y = body_mass_g, col=species) ) + geom_boxplot() +
labs(title = "Body_mass", x= "species", y = "Body_mass(g)")
x = species, fill = species
ggplot(data = penguins, aes(x=species, y = body_mass_g, fill=species) ) +
geom_boxplot() +
labs(title = "Body_mass", x= "species", y = "Body_mass(g)")
scale_fill_manual(values = group_colors)
# 그룹별로 색상 설정
group_colors <- c("Adelie" = "red", "Chinstrap" = "blue", "Gentoo" = "green")
# 박스 플롯 그리기
ggplot(data = penguins, aes(x = species, y = body_mass_g, fill = species)) +
geom_boxplot() +
scale_fill_manual(values = group_colors) +
labs(title = "Body_mass", x = "Species", y = "Body_mass(g)")
이상치 포함 여부 : outlier.shape
이상치 포함 : 기본 그래프
ggplot(data = outer_eg_df, aes(y = outer_eg) ) +
geom_boxplot() +
labs(title = "Body_mass (이상치 포함 : 기본 그래프)", x = "Species", y = "Body_mass(g)")
이상치 포함 : 다른 색상으로 설정 (outlier.colour)
ggplot(data = outer_eg_df, aes(y = outer_eg) ) +
geom_boxplot(outlier.colour = "red") +
labs(title = "Body_mass (이상치 포함 : 다른 색상으로 표시)", x = "Species", y = "Body_mass(g)")
이상치 포함 : 다른 크기로 설정 (outlier.size)
ggplot(data = outer_eg_df, aes(y = outer_eg) ) +
geom_boxplot(outlier.size = 4) + # 이상치 크기를 4로 설정
labs(title = "Body_mass (이상치 포함 : 다른 크기로 표시)", x = "Species", y = "Body_mass(g)")
이상치 포함 : 다른 도형으로 설정 (outlier.shape)
ggplot(data = outer_eg_df, aes(y = outer_eg) ) +
geom_boxplot(outlier.shape = 5) + # 5는 별표 모양
labs(title = "Body_mass (이상치 포함 : 다른 도형으로 표시)", x = "Species", y = "Body_mass(g)")
이상치 미포함 : outlier.shape = ""
ggplot(data = outer_eg_df, aes(y = outer_eg) ) +
geom_boxplot(outlier.shape = "") +
labs(title = "Body_mass (이상치 미포함)", x = "Species", y = "Body_mass(g)")
'데이터 분석 (with Rstudio)' 카테고리의 다른 글
[Rstudio] 막대 그래프 (bar plot, column plot) with ggplot (0) | 2023.10.16 |
---|---|
[Rstudio] 한 화면에 여러 개 복수 그래프 출력 (par, plot_grid) (0) | 2023.10.15 |
[Rstudio] histogram 히스토그램 with ggplot (1) | 2023.10.10 |
[Rstudio] ggplot과 grammer of graphics (0) | 2023.10.10 |
[Rstudio] 데이터 유형별 그래프 선택 기준 및 R 그래프 함수 (0) | 2023.10.09 |