excelsu의 공부 기록 블로그

R 에서 평균 비교 그래프 그리기(라이브러리 설치없이) 본문

R

R 에서 평균 비교 그래프 그리기(라이브러리 설치없이)

excelsu 2022. 6. 17. 17:55

보통 평균 비교를 위해 보통 밀도함수 그래프를 사용하지만 

라이브러리없이 쉽고 빠르게 두 집단에 대해 분포를 확인하기 위해

히스토그램으로 작성하는 코드입니다.

#데이터 불러오기, header=TRUE로 첫째줄 컬럼명 지정
rawdata=read.csv(file=choose.files(), header=TRUE)

#x축 범위,구간 지정
x_range=seq(0, 45, by=3)

a=hist(rawdata$"컬럼명", breaks=x_range, plot=FALSE)
b=hist(rawdata$"컬럼명2", breaks=x_range, plot=FALSE)

#그래프 그리기
plot(a, col=abjustcolor("blue", alpha=0.5), ann=FALSE, axes=FALSE, ylim=c(0,y_max))
plot(b, col=abjustcolor("red", alpha=0.5), add=TRUE)
title(main="제목", xlab="x축이름", ylab="y축이름", cex.main=1.6)

#축 그리기
x_axis_tick=x_range
axis(side=1, at=x_axis_tick)

y_max=max(max(a$count), max(b$count)) #y축 길이
y_axis_tick=seq(0,y_max, by=3)
axis(side=2, at=y_axis_tick)

legend("topright", c("범례1","범례2"), fill=c(adjustcolor("blue",alpha=0.5),adjustcolor("red",alpha=0.5)))

#그래프 태두리
box("figure",col="gray")