[R 통계_ggplot2 패키지] 06. geom_violin으로 데이터 분포 차트 그리기
바이올린 플롯(Violine Plot)는 데이터의 분포 형태를 보여줄 수 있다는 점에서 유용하다.
바이올린 플롯(Violine Plot)를 옆으로 눞혀, 반을 가리고 보면, 다음과 같이 변수별 데이터 분포를 확인할 수 있다.
08-1. 바이올린 차트 (기본형)
그래프1 <- ggplot(데이터명, aes(factor(x축), y축))
그래프1 <- 그래프1 + geom_violin()
#08-1. 바이올린 플롯
violin_layer1 <- ggplot(airquality, aes(x = factor(Month), y = Temp))
violin_plot1 <- violin_layer1 + geom_violin()
violin_plot1
08-2. 바이올린 차트 (색상 추가)
그래프1 <- ggplot(데이터명, aes(factor(x축), y축, fill = factor(색상변수))
그래프1 <- 그래프1 + geom_violin(alpha = 투명도)
#08-2. 바이올린 플롯(색상 추가)
violin_layer2 <- ggplot(airquality, aes(x = factor(Month), y = Temp, fill = factor(Month)))
violin_plot2 <- violin_layer2 + geom_violin(alpha = 0.3)
violin_plot2
08-3. 바이올린 차트 (평균 추가)
그래프1 <- ggplot(데이터명, aes(factor(x축), y축, fill = factor(색상변수)))
그래프1 <- 그래프1 + geom_violin(alpha = 투명도)
그래프1 <- 그래프1 + stat_summary(fun = mean, geom = "point", shape = 숫자, fill = "색상")
#08-3. 바이올린 플롯(평균 추가)
violin_layer3 <- ggplot(airquality, aes(x = factor(Month), y = Temp, fill = factor(Month)))
violin_plot3 <- violin_layer3 + geom_violin(alpha = 0.3)
violin_plot_mean <- violin_plot3 + stat_summary(fun = mean, geom = "point", shape = 23, size = 2, fill = "red")
violin_plot_mean
08-4. 바이올린 차트 (평균과 사분위 추가)
그래프1 <- ggplot(데이터명, aes(factor(x축), y축, fill = factor(색상변수), alpha = 투명도))
그래프1 <- 그래프1 + geom_violin(draw_quantiles = c (0.25, 0.75), linetype = "dashed")
그래프1 <- 그래프1 + geom_violin(draw_quantiles = c (0.5))
그래프1 <- 그래프1 + stat_summary(fun = mean, geom = "point", shape = 숫자, fill = "색상")
#08-4. 바이올린 플롯(사분위 + 평균 추가)
violin_layer4 <- ggplot(airquality, aes(x = factor(Month), y = Temp, fill = factor(Month), alpha = 0.3))
violin_plot4 <- violin_layer4 + geom_violin(draw_quantiles = c(0.25, 0.75), linetype = "dashed") +
geom_violin(draw_quantiles = c(0.5))+
stat_summary(fun = mean, geom = "point", shape = 23, size = 2, fill = "red")
violin_plot4
ggplot2 violin plot : Quick start guide - R software and data visualization - Easy Guides - Wiki - STHDA
Statistical tools for data analysis and visualization
www.sthda.com