# plot() plot((0:20)*pi/10, sin((0:20)*pi/10)) plot((1:30)*0.92, sin((1:30)*0.92)) # Scatter plots elasticband <- data.frame(stretch=c(46,54,48,50,44,42,52), distance=c(148,182,173,166,109,141,166)) elasticband attach(elasticband) # R now knows where to find distance & stretch plot(distance ~ stretch) detach(elasticband) # line graph austpop<-read.csv("c:/RData/austpop.csv") plot(ACT ~ Year, data=austpop) plot(ACT ~ Year, data=austpop, type="l") plot(ACT ~ Year, data=austpop, type="b") plot(ACT ~ Year, data=austpop, type="c") plot(ACT ~ Year, data=austpop, type="o") plot(ACT ~ Year, data=austpop, type="h") plot(ACT ~ Year, data=austpop, type="s") plot(ACT ~ Year, data=austpop, type="S") plot(ACT ~ Year, data=austpop, type="n") rocket<-read.csv(file="C:/RData/rocket.csv", header=T) rocket head(rocket) names(rocket) dim(rocket) plot(rocket$age, rocket$strength) # make a scatter plot attach(rocket) par(mfrow=c(2,2)) plot(age, strength) # points are circles plot(age, strength, pch=16) # points are solid circles plot(age, strength, pch=16, cex=2) # cex control size plot(age, strength, pch=16, cex=2, col=2) # col control colour par(mfrow=c(2,2)) plot(age, strength, pch=16, cex=2, cex.axis=1.5) plot(age, strength, pch=16, cex=2, cex.lab=1.5) plot(age, strength, pch=16, cex=2) title(main="Scatter plot", cex.main=1.5) plot(age, strength, pch=16, cex=2) title(main="Scatter plot", cex.main=1.5, sub="rocket data", cex.sub=1.3) par(mfrow=c(1,1)) plot(1,1, xlim=c(1,7), ylim=c(1.75, 5.75), type="n", axes=F, xlab="", ylab="") # do not plot pints box() points(1:7, rep(5.5,7), cex=3, pch=0:6) text(1:7,rep(5,7), paste(0:6), cex=3) points(1:7, rep(4.5,7), cex=3, pch=(0:6)+7) text((1:7),rep(4,7), paste((0:6)+7), cex=3) points(1:7, rep(3.5,7), cex=3, pch=(0:6)+14) text((1:7),rep(3,7), paste((0:6)+14), cex=3) points(1:7, rep(2.5,7), cex=3, pch=(0:6)+21) text((1:7),rep(2,7), paste((0:6)+21), cex=3) par(mfrow=c(2,2)) plot(age, strength, pch=15, cex=2, col=2, cex.lab=1.5) plot(age, strength, pch=19, cex=2, col=2, cex.lab=1.5) plot(age, strength, pch=8, cex=2, col=2, cex.lab=1.5) plot(age, strength, pch=3, cex=2, col=2, cex.lab=1.5) par(mfrow=c(2,2)) plot(age, strength, pch=19, cex=2, cex.lab=1.5, main="Scatter plot", font.main=1, col.main=2) plot(age, strength, pch=19, cex=2, cex.lab=1.5, main="Scatter plot", font.main=2, col.main=2) plot(age, strength, pch=19, cex=2, cex.lab=1.5, main="Scatter plot", font.main=3, col.main=2) plot(age, strength, pch=19, cex=2, cex.lab=1.5, main="Scatter plot", font.main=4, col.main=2) par(mfrow=c(1,1)) plot(age, strength, pch=19, cex=1, col=2, xlab="age of propellant", ylab="shear strength", main="Scatter plot", cex.lab=1.5) # add title plot(age, strength, pch=2, cex=2, col=4, xlab="age of propellant", ylab="shear strength", cex.lab=1.5) title(main="Scatter plot", cex.main=2, font.main=4, col.main="orange") # adding text in the margin mtext(side=3, line=0, text="rocket data", cex=1.5, col="purple") plot(age, strength, pch=2, cex=2, col=4, xlab="age of propellant", ylab="shear strength", cex.lab=1.5) title(main="Scatter plot", cex.main=2, font.main=4, col.main="orange") mtext(side=1, line=0, text="rocket data", cex=1.5, col="purple") plot(age, strength, pch=2, cex=2, col=4, xlab="age of propellant", ylab="shear strength", cex.lab=1.5) title(main="Scatter plot", cex.main=2, font.main=4, col.main="orange") mtext(side=2, line=0, text="rocket data", cex=1.5, col="purple") plot(age, strength, pch=2, cex=2, col=4, xlab="age of propellant", ylab="shear strength", cex.lab=1.5) title(main="Scatter plot", cex.main=2, font.main=4, col.main="orange") mtext(side=4, line=0, text="rocket data", cex=1.5, col="purple") par(mfrow=c(2,2), pch=16) library(MASS) attach(Animals) # This dataset is in the MASS package, which must be attached plot(body, brain) plot(sqrt(body), sqrt(brain)) plot((body)^0.1, (brain)^0.1) plot(log(body),log(brain)) detach(Animals) par(mfrow=c(1,1), pch=1) # Restore to 1 figure per page # Adding points, lines and text attach(Animals) Animals row.names(Animals) plot(body, brain, pch=19, cex=2, col=4, xlim=c(0,100000)) text(x=body, y=brain, labels=row.names(Animals), pos=4) # pos=4 positions text to the right of the point plot(body, brain, pch=19, cex=2, col=2, xlim=c(-20000,90000)) text(x=body, y=brain, labels=row.names(Animals), pos=2) # pos=4 positions text to the left of the point # identify() labels points plot(body, brain, pch=19, cex=2, col=4, xlim=c(0,100000)) identify(x=body, y=brain) plot(body, brain, pch=19, cex=2, col=2, xlim=c(0,100000)) identify(x=body, y=brain, row.names(Animals)) # Location on the Figure Region plot(body, brain, pch=19, cex=2, col=2, xlab="Body weight (kg)", ylab="Brain weight (g)") locator() # Plotting Mathematical Symbols x <- 1:15 y <- pi*x^2 plot(x, y, xlab="Radius", ylab=expression(Area == pi*r^2))