본문 바로가기
IT이야기/R 언어

R언어 - 그래프 만들기

by 행복찾아3만리 2021. 8. 21.
반응형

R언어를 활용하여 시각화에 필요한 그래프를 그려보도록 하겠습니다.

 

#산점도
library(MASS)
data("Cars93")
plot(Cars93$Length, Cars93$Weight)
plot(Cars93$Length, Cars93$Weight, main="제목", xlab="X축", ylab="y축")#제목
range(Cars93$Length) #141 219
range(Cars93$Weight) #1695 4105
plot(Cars93$Length, Cars93$Weight, xlim=c(141,219), ylim=c(1600,4500))#xlim, ylim 범위
plot(Cars93$Length, Cars93$Weight, pch=19)#점 모양
plot(Cars93$Length, Cars93$Weight, cex=5)#점 크기
plot(Cars93$Length, Cars93$Weight, col='red')#색상 red blue yellow
tapply(Cars93$Length, Cars93$Weight, mean)
plot(tapply(Cars93$Length, Cars93$Weight, mean), type='o')#그래프종류 p(점), l(선), b(점선), o(점과 선 중첩), n(그래프 초기화)
plot(tapply(Cars93$Length, Cars93$Weight, mean), type='o', lty=2)#선종류 

#그래프서식
par(mfrow=c(1,2))
plot(tapply(Cars93$Length, Cars93$Weight, mean), type='o')#그래프종류 p(점), l(선), b(점선), o(점과 선 중첩), n(그래프 초기화)
plot(tapply(Cars93$Length, Cars93$Weight, mean), type='o', lty=2)#선종류 

#범례
plot(Cars93$Length, Cars93$Weight)
legend('bottom', c("x1","y1"))#bottom, left, top, right, center
legend(180,4000, c("x1","y1"), pch=3:4, lty=5:1, title="범례례")

#점그래프
plot(NULL, type='n', xlim=c(0,8), ylim=c(0,3), xlab='petal.length', ylab='sepal.length', main='iris')#빈 plot 그리고
points(iris$Petal.Length, iris$Petal.Width, cex=0.5)#점 찍기

#선 그래프
plot(NULL, type='n', xlim=c(0,8), ylim=c(0,3), xlab='petal.length', ylab='sepal.length', main='iris')#빈 plot 그리고
lines(c(0,3), c(0,1))
lines(c(3,5), c(2,3), lty=1)#lty 선종류
lines(c(2,4), c(1,2), lty=2, lwd=5)#lwd 두깨

plot(cars)
lines(lowess(cars))#지역 가중 다항식 회귀

#직선 그래프
plot(cars)
cars_lm<-lm(dist~speed, data=cars)
abline(cars_lm, col='blue')
abline(v=median(cars$speed), lty=3)
abline(h=median(cars$dist), lty=3)
abline(v=20,h=50, lty=3)


#곡선그래프
curve(dnorm(x, mean=0, sd=1), from=-3, to=3, xlab='x', ylab='density', main='curve of dnorm')

#막대그래프
barplot(c(23,20))
barplot(table(Cars93$Origin), ylim=c(1,50), xlab='Origin', ylab='도수', main='막대그래프')
barplot(table(Cars93$Origin, Cars93$Cylinders), beside=T, main='막대그래프', legend=T)#beside는 하나의 막대로 나오는거, legend는 범례

#히스토그램
hist(iris$Petal.Length, breaks=5)

#파이차트
pie(table(Cars93$Cylinders))

#산점도 행렬
pairs(iris)
pairs(~Sepal.Length+Sepal.Width,data=iris)

 

여기까지 R언어를 활용한 그래프 만드는 방법에 대해 정리 했습니다.

반응형

댓글