Presentation is loading. Please wait.

Presentation is loading. Please wait.

R 그래픽스 5 : R을 이용한 도형그리기 (1) 원(circle) 그리기

Similar presentations


Presentation on theme: "R 그래픽스 5 : R을 이용한 도형그리기 (1) 원(circle) 그리기"— Presentation transcript:

1 R 그래픽스 5 : R을 이용한 도형그리기 (1) 원(circle) 그리기
par(pty="s") # “s” for small angle <- (0:360)*pi/ # method 1 angle <- seq(-pi, pi, length=361) # method 2 x <- 3+5*cos(angle) y <- 4+5*sin(angle) plot(x,y, type="l", main= "circle with radius 5 and center (3,4)") R Graphics : Device 2 (ACTIVE)

2 R 그래픽스 5 : R을 이용한 도형그리기 (2) 대칭이동 그래프 그리기
par(oma=c(0,0,2,0)) plot(-5:5, -5:5, type="n", xlab="", ylab="") abline(v=0) abline(h=0) mtext("y", side=3, line=1, cex=2) mtext("x", side=4, line=1, cex=2) # original x1 <- 1:4 y1 <- c(1,4,2,3) lines(x1,y1, lty=1, col=1) text(4,4, "original", cex=2) # wrt x x2 <- x1 y2 <- -y1 lines(x2,y2, lty=2, col=2) text(4,-4, "wrt x", cex=2) # wrt y x3 <- -x1 y3 <- y1 lines(x3,y3, lty=3, col=3) text(-4,4, "wrt y", cex=2) # wrt (0,0) x4 <- -x1 y4 <- -y1 lines(x4,y4, lty=4, col=4) text(-4,-4, "wrt (0,0)", cex=2) title(main="symmetry", line=0, outer=T) R Graphics : Device 2 (ACTIVE)

3 R 그래픽스 5 : R을 이용한 도형그리기 (3) 좌표 회전(rotation)하기
par(oma=c(0,0,2,0)) plot(-5:5, -5:5, type="n", xlab="", ylab="") abline(v=0) abline(h=0) mtext("y", side=3, line=1, cex=2) mtext("x", side=4, line=1, cex=2) lines(x1, y1, lty=1, col=1) text(4,4, "original", cex=1.5) # 90 degrees rotation angle1 <- 90/180*pi # radian angle x2 <- x1*cos(angle1)- y1*sin(angle1) y2 <- x1*sin(angle1)+ y1*cos(angle1) lines(x2, y2, lty=2, col=2) text(-4,4, "90 degrees", cex=1.5) # 180 degrees rotation angle2 <- 180/180*pi # radian angle x3 <- x1*cos(angle2)- y1*sin(angle2) y3 <- x1*sin(angle2)+ y1*cos(angle2) lines(x3, y3, lty=3, col=3) text(-4,-4, "180 degrees", cex=1.5) # 270 degrees rotation angle3 <- 270/180*pi # radian angle x4 <- x1*cos(angle3)- y1*sin(angle3) y4 <- x1*sin(angle3)+ y1*cos(angle3) lines(x4, y4, lty=4, col=4) text(4,-4, "270 degrees", cex=1.5) title(main="rotation",line=0, outer=T) R Graphics : Device 2 (ACTIVE)

4 R 그래픽스 5 : R을 이용한 도형그리기 (4) 다각형(polygon) 그리기
par(oma=c(0,0,2,0), mfrow=c(2,2),pty="s") # triangle theta <- seq(pi/2, pi/2+2*pi, by=2*pi/3) tri.x <- cos(theta) tri.y <- sin(theta) plot(tri.x, tri.y, type="l", xlim=c(-1,1), ylim=c(-1,1), main="triangle") # square theta <- seq(pi/4, pi/4+2*pi, by=2*pi/4) sq.x <- cos(theta) sq.y <- sin(theta) plot(sq.x, sq.y, type="l", xlim=c(-1,1), ylim=c(-1,1), main="square") # pentagon theta <- seq(pi/2, pi/2+2*pi, by=2*pi/5) pent.x <- cos(theta) pent.y <- sin(theta) plot(pent.x, pent.y, type="l", xlim=c(-1,1), ylim=c(-1,1), main="pentagon") # star s <- seq(length(pent.x)) s <- c(s[s%%2==1], s[s%%2==0]) # line 순서를 지정하는 벡터 plot(pent.x, pent.y, type="n", xlim=c(-1,1), ylim=c(-1,1), main="star shape") lines(pent.x[s], pent.y[s]) title(main="drawing polygon", line=0, outer=T) # main title R Graphics : Device 2 (ACTIVE)

5 R 그래픽스 5 : R을 이용한 도형그리기 (5) 확대(enlargement) 하기
R Graphics : Device 2 (ACTIVE) par(oma=c(0,0,0,0), mfrow=c(1,1), pty="s") angle <- (0:360)*pi/180 x1 <- 3+5*cos(angle) y1 <- 4+5*sin(angle) x2 <- (x1-1)*3+1 y2 <- (y1-2)*3+2 x3 <- (x1-1)*sqrt(3)+1 y3 <- (y1-2)*sqrt(3)+2 plot(x1, y1, type="l", xlim=c(-10,25), ylim=c(-10,25)) lines(x2,y2, lty=2) lines(x3,y3, lty=3) points(1,2, pch=3, cex=2) title("Enlargement of Circle")

6 R 그래픽스 5 : R을 이용한 도형그리기 (6) 타원(ellipse) 그리기
par(mfrow=c(1,1), pty="s") x1 <- (-20:80)/10 y1 <- sqrt(1-(x1-3)^2/25)*3+4 y2 <- -sqrt(1-(x1-3)^2/25)*3+4 plot(x1,y1, type="l", ylim=c(-1,9)) lines(x1,y2, lty=2) points(3,4, pch=3) title("Ellipse1“) theta <- seq(-pi, pi, length=181) theta <- seq(-pi, pi, by=2*pi/181) theta <- (0:180)*pi/90 x <- 5*cos(theta) y <- 3*sin(theta) plot(x,y, type="l", ylim=c(-5,5),main="Ellipse2") theta <- 45*pi/180 x1 <- x*cos(theta)-y*sin(theta) y1 <- x*sin(theta)+y*cos(theta) plot(x,y, type="l", xlim=c(-5,5),ylim=c(-5,5)) abline(h=0) lines(x1,y1, lty=2) abline(a=0,b=1, lty=1) title(expression(45*degree~~"rotation"))

7 R 그래픽스 5 : R을 이용한 도형그리기 (7) curve 함수
par(mfrow=c(2,2)) curve(x^3-3*x, -2, 2) title(main="User defined expression") myfun <- function(x) {x^2+2} curve(myfun, -pi, pi) #(2) User Function title(main="User defined function") curve(dnorm, from=-3, to=3) #(3) R function title(main="Normal distribution density") plot(dnorm, from=-3, to=3) #(4) plot R function title(main="curve by plot function") curve(sin, from=-2*pi, to=2*pi, lty=1, col="red") curve(cos, from=-2*pi, to=2*pi, lty=2, col="blue", add=T) title("add=TRUE") curve(dnorm, from=-3, to=3, log="y") title(main="dnorm by log=\"y\"") R Graphics : Device 2 (ACTIVE)

8 R 그래픽스 5 : R을 이용한 도형그리기 (8) persp 함수
x <- seq(-10,10, length=30) y <- x f <- function(x,y){r <- sqrt(x^2+y^2); 10*sin(r)/r} z <- outer(x,y,f) z[is.na(z)] <- 1 #결측치를 1로 바꾼다. persp(x,y,z, theta=30, phi=30, expand=0.5, col="lightblue", ltheta=120, shade=0.75, ticktype="detailed", xlab="X", ylab="Y", zlab="Sinc(r)") -> res title(main="Perspective Plots with Sinc Function") R Graphics : Device 2 (ACTIVE)

9


Download ppt "R 그래픽스 5 : R을 이용한 도형그리기 (1) 원(circle) 그리기"

Similar presentations


Ads by Google