본문 바로가기
R

R ) 독학 :: 산점도 알아보기 scatter plot

by C.Mond 2022. 5. 8.
728x90
728x90

산점도 그리는것에 대해 공부한것을 기록하려고 합니다.

 

ggplot(data=iris, aes(x=Sepal.Length, y=Sepal.Width,color=Species))+
geom_point(shape=17, size=3)+
ggtitle("Scatter Sepal_Length,Width")+
geom_text(aes(label=Species, size=2, vjust=-1, hjust=0))

위와 같이 코드를 작성하면 Species별로 색을 나눠 입힐 수 있습니다.

 

또한 scatter plot에 있는 point의 모양도 바꿀 수 있습니다.

 

모양은 아래와 같이 정할 수 있습니다.

ggplot(data=iris, aes(x=Sepal.Length, y=Sepal.Width,shape=Species,color=Species))+
geom_point( size=3)+
ggtitle("Scatter Sepal_Length,Width")+
geom_text(aes(label=Species, size=2, vjust=-1, hjust=0))

Species별로 shape를 다르게 할 수도 있습니다.

ggplot(data=iris, aes(x=Sepal.Length, y=Sepal.Width)) +
geom_point(size=3, shape=17) +
facet_grid(Species ~.) +
ggtitle("facet_grid를 사용")

facet_grid를 이용해 Species별로 plot을 그릴 수도 있습니다.

ggplot(data=iris, aes(x=Sepal.Length, y=Sepal.Width)) +
geom_point(shape=17, size=3) +
stat_smooth(method=lm,se=F)

선형 회귀선도 추가할 수 있습니다

728x90
728x90

댓글