2019년 8월 19일 월요일

Chp2. GRAPH - 2

본론
2-1-1 고수준 작도함수
mfrow 를 이용한 한화면에 여러 그래프 그리기
par (mfrow = c(nr,nc))
-->nr 행의수 nc 열의 수직선

m <- par (mfrow=c(1,3))
apple <- c(10,12,14,16,18,20)
banana <-c(11,13,15,17,19,21)
berry <-c(9,12,14,16,15,19)

plot(apple,type="o")
plot(banana,type="s")
plot(berry,type="o")
savePlot("2-1-1-11.png",type="png")


sin cos 그래프 중첩해서 그리기

plot(sin,-pi,pi,xlab="",lty=2)
par(new=T)
plot(cos,-pi,pi,xlab="x",ylab="y")
plot(sin,-pi,pi,xlab="x",ylab="y",lty=2)
plot(cos,-pi,pi,add=T)
savePlot("2-1-1-12.png",type="png")


apple <-c(260,300,250,280,310)
peach <-c(180,200,210,190,170)
berry <-c(210,250,260,210,220)
plot(apple,type="o",col="red",ylim=c(0,400),axes=F,ann=F)
axis(1,at=1:6,lab=c("월","화","수","목","금","토"))
axis(2,ylim=c(0,400))
title(main="Fruits",col.main="red",font.main=4)
title(xlab="요일",col.lab="black")
title(ylab="가격",col.lab="blue")
lines(berry,type="o",pch=21,col="green",lty=2)
lines(peach,type="o",pch=22,col="blue",lty=2)
savePlot("2-1-1-13.png",type="png")


2-1-2 barplot() 막대그래프

x <- matrix(c(0.3,0.7,0.8,0.2),2,2)
barplot(x,beside=T,names=1:2)
savePlot("2-1-1-14.png",type="png")


x <- matrix(c(0.3,0.7,0.8,0.2),2,2)
barplot(x,horiz=T,names=1:2)
savePlot("2-1-1-15.png",type="png")


apple <-c(260,300,250,280,310)
peach <-c(180,200,210,190,170)
berry <-c(210,250,260,210,220)

barplot(apple,main="Fruits",xlab="요일",ylab="수량",names.arg=c("월","화","수","목","금"),border="red",density=c(10,20,30,40,50))
savePlot("2-1-1-16.png",type="png")


fruits10 <-read.table("fruits10.txt",header=T)


> fruits10 <-read.table("fruits10",header=T)
> fruits10
  apple peach berry
1   260   180   210
2   300   200   250
3   250   210   260
4   280   190   210
5   310   170   220

barplot(as.matrix(fruits10),main="fruits",ylab="수량",beside=T,col=rainbow(5),ylim=(c(0,400)))
savePlot("2-1-1-17.png",type="png")


barplot(t(fruits10),main="fruits",ylab="판매량",ylim=c(0,1000),col=rainbow(3),space=0.1,cex.axis=0.8,las=1,names.arg=c("월","화","수","목","금"),cex=0.8)
legend(4.5,1000,names(fruits10),cex=0.8,fill=rainbow(3))
savePlot("2-1-1-18.png",type="png")


apple의값이 300보다 크거나 같은경우만 빨간색으로
colors <-c()
for(i in 1:length(apple)){
if(apple[i] >= 300){
colors <-c(colors,"red") }
else {
colors <-c(colors,"green")}
}
barplot(apple,main="Apple",xlab="요일",ylab="수량",names.arg=c("월","화","수","목","금"),col=colors)
savePlot("2-1-1-19.png",type="png")

2-1-3 Dot chart() 점으로 표시하는것
x <-c(1:10)
dotchart(x,labels=paste("Test",1:10))
savePlot("2-1-1-20.png",type="png")

댓글 없음:

댓글 쓰기