본문 바로가기
2. 그래프 그리기 (고수준 함수)/plot() - 산점도

R 산점도 그리는 방법, plot()

by makhimh 2019. 11. 28.
반응형

R 산점도 그리는 방법, plot()


#1. 방법


plot(x, y, ...)


x : x좌표

y : y좌표



#2. 예제


1) 기본형


x=c(1,2,3,4,5)

y=c(1,2,3,2,1)

plot(x,y)




2) 실 사용


plot 함수를 사용할 때, 보통 여러 그룹의 좌표를 입력해야합니다. 코드를 효과적으로 관리하기 위해서 저수준함수인 points를 이용합니다. 비어있는 plot을 그려놓고, points함수로 그룹별 데이터를 입력하는 것입니다.


#데이터 생성

height_male=rnorm(50,175,10)

weight_male=rnorm(50,70,5)

height_female=rnorm(50,160,10)

weight_female=rnorm(50,50,5)


#범위만 설정되어 있는 plot 생성. 

#ann=FALSE 로 축이름 제거. 

#type="n" 으로 데이터 표시 제거. 

plot(1,type="n",ann=FALSE,

     xlim=c(120,200),

     ylim=c(30,90))


#데이터 입력

points(height_male,weight_male,col='red')

points(height_female,weight_female,col='blue')


#그래프제목, 축이름 입력

title(main="height and weight",

      xlab="height",

      ylab="weight")


#범례 입력

legend(120,90,c("남자","여자"),fill=c("red","blue"))


반응형

댓글