본문 바로가기
my_lesson/_R_컴퓨팅

R_컴퓨팅 - 그래프 그리기

by boolean 2019. 2. 19.
728x90

R_컴퓨팅 - 그래프 그리기

>BMI <- read.table("BMI파일경로/BMI.txt", header = T)    # BMI파일 다운로드

막대그림

-범주형 자료(순서형 또는 명목형 자료)의 빈도수 또는 상대도수를 기동의 높이로 표현하기 위한 그림으로 아래 함수를 사용


>barplot.test1 <- function() {

+par(mfrow = c(1, 2))    # 1행 2열로 표시

+barplot(table(BMI$religion))

+barplot(table(BMI$gender, BMI$religion))

+}    # end function

>barplot.test1()   #     함수 호출

#  빈도수 또는 상대도수 그래프를 그림 출력한다.




>barplot.test2 <- function () {

+par{mfrow = c(1, 2))

+a <- c(10, 20, 30); b <- c(10, 10, 20)

+barplot(cbind(a, b))

+barplot(cbind(a, b), beside = T) # 각행의 빈도수를 beside로 나타나게 한다.

+}     #end function

>barplot.test2()



원그림

-범주형 자료의 상대빈도수를 원의 면적(또는 각도)에 비례하게 그린다.


>pie.test <- function() {

+par(mfrow = c(1, 2))

+pie(rep(1, 7), col = rainbow(7), radius = 0.9)

+pie(table(BMI$religion))

+}     #    end function

>pie.test()



상자그림

사분위수 표현


boxplot.test2 <- function() {

+par(mfrow = c(1, 2))

+boxplot(BMI$height))

+boxplot(height~gender, data = BMI, col = rainbow(2))

+}     #    end function


출기 잎 그림

자료의 수가 많지 않을 때 전체 자료를 줄기와 잎으로 패현한다.


>tem(BMI$height)

>stem(BMI$height, scale = 0.4)     # scale: 줄기의 키기 과 width:잎의 수 조절 40% 줄임



점도표

자료의 수가 많지 않을 경우 점도표를 사용하여 자료의 분포를 확인한다.


>stripchart.test <- function() {

+par(mfrow = c(1, 3))

+stripchart(BMI$weight~!BMI$gender, method = "stack")

+stripchart(BMI$weight~BMI$gender, method = "jitter")

+stripchart(weight~gender, data = BMI, method = "stack", pch = 24:25, col = c("red", "blue")) #pch : 패턴

+}    #    end function



히스토그램

연속인 변수의 분포를 알기 위해서 변수의 값을 적절한 범위로 구분하여 해당 범위에 속하는 자료의 빈도 또는 상대도수를 기둥의 높이로 하여 생성한다.


>hist(BMI$height, labels = T, ylim = c(0, 80))     #     ylim(y축의크기)

>hist(BMI$height, labels = T, freq = F, ylim = c(0, 0.08))    #     freq(상대도수로 표현)


산점도

다른 변수와의 관계를 그래프로 표현한다.


>plot.test <- function() {

+par(mfrow = c(2, 2))

+x <- seq(1, 2*pi, length = 10)

y <- sin(x)

+plot(x, y, type = "l", main = "type = \"l\"")     # \(특수문자 표시 접두어)

+plot(x, y, type = "b", main = "type = \"b\"")

+plot(x, y, type = "h", main = "type = \"h\"")

+plot(x, y, type = "c", main = "type = \"c\"")

+}    #    end function


fix(함수명) R에서 함수편집


>plot.test2 <- function() {

+par(mfrow = c(1, 2))

+plot(BMI$height, BMI$weight, main = "키와 몸무게의 산점도", type = "p", pch = ".", sub = "키(cm), 몽무게(kg)")

+plot(BMI$height, BMI$weight, main = "키와 몸무게의 산점도", type = "p", pch = "0", sub = "키(cm), 몸무게(kg)", 

+    xlab = "몸무게", ylab = "키")

+}    # end function



>plot(BMI)     #     BMI 객체의 모든 가능한 조합에 대한 산점도를 나타내준다.



abline 함수

그릴 직선의 기울기와 절편을 이용해  선을 그린다.


abline(a = NULL, b = NULL, h = NULL, v = NULL, coef = NULL, ....)

- a, b : 그릴 직선의 y절편 및 기울기

- h : 수평선을 그릴 때 y값만 설정하며 이 때 y값을 h에 설정

- v : 수직선을 그릴 때 x축의 값

- coef : 위의 a, b 값을 벡터로 설정할 때 사용  coef = c(a, b)


>abline.test <- function() {

+ht <- BMI$height

+wt <- BMI$weight

+plot(ht, wt)

+abline(v = mean(ht), lty = 2, col = "blue")

+abline(h = mean(wt), lty = 2, col = "blue")

+abline(lsfit(ht, wt), lwd = 2, col = "red")

+abline(lsfit(ht, wt)$coef, lw = 1, col = "white")    # lsfit(회귀직선)

+}     #    end function


arrow와 segment


arrow.test <- function() {

+ ht <- BMI$height; meanht <- mean(ht); minht <- min(ht);

+ maxht <- max(ht)

+ wt <- BMI$weight; meanwt <- mean(wt); minwt <- min(wt);

+ maxwt <- max(wt)

+ plot( ht, wt)

+ arrows(minht, meanwt, maxht, meanwt, lty = 2, col = "blue")

+ arrows(meanht, minwt, meanht, maxwt, lty = 2, length = 0.1, col = "blue")

+ # 회귀직선이 y = -84.1 + 0.85x로 추정됨.

+ segments(minht, -84.1 + 0.85 * minth, maxht, -84.1 + 0.85 * maxht, lwd = 2, col = "red")

+}    #end function

댓글