안녕하세요 구도입니다.


오늘은 Plot 그래프 그리기, scatter plot 그리기, 수치형 자료를 범주형으로 바꾸는 방법에 대해 알아 보는 시간을 가지도록 하겠습니다.


작성한 코드만 보고 이해하시지 말고 프로그램에서 직접 실습해보시면서 공부하시면 이해가 훨씬 빠르시답니다.


*프로그램은 'R x64 3.5.1' 을 사용하였습니다*


install.packages("graphics")

library(graphics)


#다양한 type으로 plot 그래프 그리기

data(women)

women


weight <- women$weight  #women 데이터 세트의 weight를 weight값으로 할당

plot(weight)  #weight 값의 그래프


height <- women$height   #women 데이터 세트의 height를 height값으로 할당

plot(height, weight, xlab="키", ylab="몸무게")  #height(x)에 따른 weight(y)의 그래프



par(mfrow=c(2,3))

plot(height, weight, xlab="키", ylab="몸무게", type="p")  # type옵션은 그래프 출력 모양의 유형 type=p는 점으로 표현

plot(height, weight, xlab="키", ylab="몸무게", type="l")  # type=l은 선으로 표현

plot(height, weight, xlab="키", ylab="몸무게", type="b")  # type=b는 점과 선으로 표현

plot(height, weight, xlab="키", ylab="몸무게", type="c")  # type=c는 b의 선으로 표현

plot(height, weight, xlab="키", ylab="몸무게", type="o")  # type=o는 점 위의 선으로 표현

plot(height, weight, xlab="키", ylab="몸무게", type="h")  # type=h는 각 값의 높이에 해당하는 수직선으로 표현



par(mfrow=c(2,2))

plot(height, weight, xlab="키", ylab="몸무게", type="s")  # type=s는 계단형으로 표현

plot(height, weight, xlab="키", ylab="몸무게", type="S")  # type=S는 계단형으로 표현

plot(height, weight, xlab="키", ylab="몸무게", type="n")  # type=n은 나타나지 않음




plot(height, weight, xlab="키", ylab="몸무게", type="l", lty=1, lwd=1)

  #lty는 선의 유형을 결정함 1~6정도 변경 가능. lwd는 선의 굵기로 디폴트는 1이며 2로 지정하면 두배가 됨


par(mfrow=c(2,3))

plot(height, weight, xlab="키", ylab="몸무게", type="l", lty=1)

plot(height, weight, xlab="키", ylab="몸무게", type="l", lty=2)

plot(height, weight, xlab="키", ylab="몸무게", type="l", lty=3)

plot(height, weight, xlab="키", ylab="몸무게", type="l", lty=4)

plot(height, weight, xlab="키", ylab="몸무게", type="l", lty=5)

plot(height, weight, xlab="키", ylab="몸무게", type="l", lty=6)


par(mfrow=c(1,2))

plot(height, weight, xlab="키", ylab="몸무게", type="l", lty=1, lwd=1)

plot(height, weight, xlab="키", ylab="몸무게", type="l", lty=1, lwd=2)



#플로팅 문자 다양하게 변화주며 scatter plot 그리기


plot(height, weight, xlab="키", ylab="몸무게")


plot(height, weight, xlab="키", ylab="몸무게", pch=0) #pch(plotting character)옵션은 0~25까지 숫자옵션 가능. 0~20까지는 col옵션만 지정하면 되고 21~25까지는

                                                       col옵션(문자의 경계선 색)과 bg옵션(문자의 내부 색) 같이 지정


par(mfrow=c(2,2))

plot(height, weight, xlab="키", ylab="몸무게", pch=0, col="blue")

plot(height, weight, xlab="키", ylab="몸무게", pch=0, col="blue", cex=1.5) #cex(character expansion)옵션은 문자의 크기. 디폴트는 1이며 1.5는 50% 더 크게, 0.5는 50% 더 작게 나타남

plot(height, weight, xlab="키", ylab="몸무게", pch=0, col="blue", cex=0.5)




par(mfrow=c(2,2))

plot(height, weight, xlab="키", ylab="몸무게", pch=21, col="blue", bg="yellow")

plot(height, weight, xlab="키", ylab="몸무게", pch=21, col="blue", bg="yellow", cex=2)

plot(height, weight, xlab="키", ylab="몸무게", pch=21, col="blue", bg="yellow", cex=0.7)


par(mfrow=c(1,2))

plot(height, weight, xlab="키", ylab="몸무게", pch="@")

plot(height, weight, xlab="키", ylab="몸무게", pch="A")




#수치 자료를 범주형 자료 변환하기

Sals = c(12, 0.4, 5, 2, 50, 8, 3, 1, 4, 0.25)  #자료의 입력

max(Sals)

min(Sals)

Seps=cut(Sals, breaks=c(0,1,5,max(Sals)))   #분류할 범위 정함

Seps

table(Seps)  # 표 구성

levels(Seps)=c("저소득", "일반", "고소득")   #수준의 이름 부여 변경

Seps    #변경된 값 확인

table(Seps)   #변경된 이름으로 표 구성


오늘도 글 읽어주셔서 감사합니다.


+ Recent posts