# # Olemasolevale graafikule elementide lisamine # # Viimati muudetud - 28. august 2005 # Koostanud: Märt Möls # # Millega png-pilte vaadata vaataja="C:\\Program Files\\Mozilla Firefox\\firefox.exe" # Kuhu pildfailid salvestada tee="F:\\kursus\\R\\temp\\lisamine\\" algus=function(pilt, xs, ys){ assign("teenimi", paste(tee,pilt,sep=""), env=.GlobalEnv) assign("teenimi2", paste('"',vaataja,'" ',tee,pilt,sep=""), env=.GlobalEnv) png(filename = teenimi, width = xs, height = ys, pointsize = 12, bg = "white") } # --------------------------------------------------- # # Joonte lisamine # # --------------------------------------------------- algus("pilt1.png", 620, 225) tunnusx=c(1,2,3,5) tunnusy1=c(6,9,10,8) tunnusy2=c(4,6,7,7) tunnusy3=c(3,6,9,10) par(plt=c(0.06,0.43,0.2,0.95)) # xlab="Aeg (päevades)", ylab="Andoksüül (mg/l)" plot(tunnusx, tunnusy1, type="l", ylim=c(0,10), xlab="", ylab="", col="green4", lwd=3) par(plt=c(0.6,0.97,0.2,0.95), new=TRUE) plot(tunnusx, tunnusy1, type="l", ylim=c(0,10), xlab="", ylab="" , col="green4", lwd=3) lines(tunnusx,tunnusy2, col="green", lwd=3) lines(tunnusx,tunnusy3, col="slateblue2", lwd=3) par(plt=c(0.45,0.54,0.475,0.675), new=TRUE) plot(0,0, type="n", xlim=c(0,1), ylim=c(0,1), xaxt="n",yaxt="n", bty="n",xlab="",ylab="") arrows(0,0.5,1,0.5, lwd=4) dev.off() system(teenimi2) # --------------------------------------------------- # # Punktide lisamine # # --------------------------------------------------- algus("pilt2.png", 620, 225) x=runif(100) y=2+3*x-4*x^2+4*x^3+rnorm(100)*0.3 x2=runif(100) y2=2.2+4*x2-4*x2^2+4*x2^3+rnorm(100)*0.3 par(plt=c(0.06,0.43,0.2,0.95)) plot(x,y, ylim=c(0,6), pch=21, col="slateblue2", bg="skyblue1", cex=1, lwd=2, xlab="", ylab="") # xlab="Küllastatus", ylab="Intensiivsus" par(plt=c(0.6,0.97,0.2,0.95), new=TRUE) plot(x,y, ylim=c(0,6), pch=21, col="slateblue2", bg="skyblue1", cex=1, lwd=2, xlab="", ylab="") points(x2,y2, pch=21, col="red", bg="yellow", cex=1) par(plt=c(0.45,0.54,0.475,0.675), new=TRUE) plot(0,0, type="n", xlim=c(0,1), ylim=c(0,1), xaxt="n",yaxt="n", bty="n",xlab="",ylab="") arrows(0,0.5,1,0.5, lwd=4) dev.off() system(teenimi2) # --------------------------------------------------- # # Teksti lisamine # # --------------------------------------------------- algus("pilt3.png", 620, 225) par(plt=c(0.06,0.43,0.2,0.95)) x=0:4 y=c(4,6,7,11,8) # par(mar=c(3,3,2,2)) plot(x,y, type="b", ylim=c(0,12),xlab="", ylab="") par(plt=c(0.6,0.97,0.2,0.95), new=TRUE) plot(x,y, type="b", ylim=c(0,12),xlab="", ylab="") text(3, 10, "Maksimum", srt=90, adj=c(1,0.5), col="darkred") text(0.1, 4, "Miinimum", adj=c(0,1), col="darkred") par(plt=c(0.45,0.54,0.475,0.675), new=TRUE) plot(0,0, type="n", xlim=c(0,1), ylim=c(0,1), xaxt="n",yaxt="n", bty="n",xlab="",ylab="") arrows(0,0.5,1,0.5, lwd=4) dev.off() system(teenimi2) # --------------------------------------------------- # # Pinna värvimine # # --------------------------------------------------- algus("pilt4.png", 620, 225) par(plt=c(0.06,0.43,0.2,0.75)) x=seq(-4,4, 0.02) y=dnorm(x, mean=0, sd=1) plot(x,y, type="l", main="Normaaljaotuse tihedus", xlim=c(-3,3), col="slateblue", lwd=2, ylim=c(0,0.45), xlab="", ylab="") par(plt=c(0.6,0.97,0.2,0.75), new=TRUE) plot(x,y, type="l", main="Normaaljaotuse tihedus", xlim=c(-3,3), col="slateblue", lwd=2, ylim=c(0,0.45), xlab="", ylab="") x1=seq(-1.96,1.96,0.02) y1=c(0,dnorm(x1, mean=0, sd=1),0,0) x1=c(-1.96,x1,1.96,-1.96) polygon(x1,y1, col="lightskyblue1", border="slateblue", lwd=2) lines(c(-4,4),c(0,0), lty=2) text(0, 0.15, "S=0,95", cex=1.2) par(plt=c(0.45,0.54,0.4,0.6), new=TRUE) plot(0,0, type="n", xlim=c(0,1), ylim=c(0,1), xaxt="n",yaxt="n", bty="n",xlab="",ylab="") arrows(0,0.5,1,0.5, lwd=4) dev.off() system(teenimi2) # --------------------------------------------------- # # Legendi lisamine # # --------------------------------------------------- algus("pilt5.png", 620, 225) par(plt=c(0.06,0.43,0.2,0.95)) x=runif(100) y=1+3*x-4*x^2+4*x^3+rnorm(100)*0.3 x2=runif(100) y2=1.2+4*x2-4*x2^2+4*x2^3+rnorm(100)*0.3 plot(x,y, ylim=c(0,6), pch=21, col="slateblue2", bg="skyblue1", cex=1, lwd=2, xlab="", ylab="") points(x2,y2, pch=21, col="red", bg="yellow", cex=1) par(plt=c(0.6,0.97,0.2,0.95), new=TRUE) plot(x,y, ylim=c(0,6), pch=21, col="slateblue2", bg="skyblue1", cex=1, lwd=2, xlab="", ylab="") points(x2,y2, pch=c(21,21), col="red", bg="yellow", cex=1) legend(0.1,5.5, legend=c("poisid","tüdrukud"), pch=21, col=c("slateblue2", "red"), pt.bg=c("skyblue1","yellow"), bg="gray98") par(plt=c(0.45,0.54,0.475,0.675), new=TRUE) plot(0,0, type="n", xlim=c(0,1), ylim=c(0,1), xaxt="n",yaxt="n", bty="n",xlab="",ylab="") arrows(0,0.5,1,0.5, lwd=4) dev.off() system(teenimi2) # --------------------------------------------------- # # Telje lisamine # # --------------------------------------------------- Tempandmed=read.csv("F:\\kursus\\R\\temp\\temp.txt", sep=",", dec=".", header=TRUE) attach(Tempandmed) # kuup = strptime(Aeg, "%Y-%m-%d %H:%M:%S") # aeg=as.Date(kuup) aeg=as.Date(Aeg) keskmTemp = by(Temperatuur, aeg, mean) # Leiame päevakeskmised temperatuurid # par(mar=c(4,4,4,4)) algus("pilt6.png", 620, 225) par(plt=c(0.06,0.36,0.2,0.80)) plot(unique(aeg), keskmTemp, type="l", main="Temperatuur Tartus 2005.a.", ylab="", cex.main=0.9, yaxt="n", col="red", lwd=2, col.main="darkred", cex.axis=0.8) mtext("http://meteo.physic.ut.ee/ -andmetel",3, line=0.5, cex=0.8, col="coral4") par(plt=c(0.60,0.90,0.2,0.80), new=TRUE) plot(unique(aeg), keskmTemp, type="l", main="Temperatuur Tartus 2005.a.", cex.main=0.9, ylab="", yaxt="n", col="red", lwd=2, col.main="darkred", cex.axis=0.8) mtext("http://meteo.physic.ut.ee/ -andmetel",3, line=0.5, cex=0.8, col="coral4") axis(4, at=(seq(0,80,10)-32)/1.8, labels=seq(0,80,10), las=2, col="darkred") mtext("Fahrenheit",4, line=2.5, cex=1.2, col="darkred") axis(2, at=seq(-20,30,10), las=2, col="darkred") mtext("Celsius",2, line=2.5, cex=1.2, col="darkred") par(plt=c(0.4,0.5,0.475,0.675), new=TRUE) plot(0,0, type="n", xlim=c(0,1), ylim=c(0,1), xaxt="n",yaxt="n", bty="n",xlab="",ylab="") arrows(0,0.5,1,0.5, lwd=4) dev.off() system(teenimi2) # --------------------------------------------------- # # Muu lisamine - nooled, ellipsid, pildid, grid, .... # # --------------------------------------------------- algus("pilt7.png", 620, 225) par(plt=c(0.06,0.43,0.2,0.9)) x=rnorm(100) y=rnorm(100)+2*x plot(x,y, xlab="",ylab="", main="") # par(plt=c(0.6,0.97,0.2,0.75), new=TRUE) par(plt=c(0.6,0.78,0.05,0.45), new=TRUE) plot(x,y, xlab="",ylab="", main="", xaxt="n", yaxt="n") data.ellipse(x,y, plot.points=FALSE) par(plt=c(0.6,0.78,0.55,0.95), new=TRUE) plot(x,y, xlab="",ylab="", main="", xaxt="n", yaxt="n") arrows(1.8,-1.8,0,01, lwd=3) par(plt=c(0.8,0.98,0.55,0.95), new=TRUE) plot(x,y, xlab="",ylab="", main="", xaxt="n", yaxt="n") grid() par(plt=c(0.8,0.98,0.05,0.45), new=TRUE) plot(x,y, xlab="",ylab="", main="", xaxt="n", yaxt="n") par(plt=c(0.9,0.97,0.05,0.25), new=TRUE) pie(c(4,6,7), labels=NA) par(plt=c(0.45,0.55,0.125,0.875), new=TRUE) plot(0,0, type="n", xlim=c(0,1), ylim=c(0,1), xaxt="n",yaxt="n", bty="n",xlab="",ylab="") arrows(0,0.52,1,0.85, lwd=4) arrows(0,0.48,1,0.15, lwd=4) dev.off() system(teenimi2) # --------------------------------------------------- # # Tühja pildi näide... # # --------------------------------------------------- algus("tyhi.png", 400, 300) plot (NA,NA, type="n", bty="n", xaxt="n", yaxt="n", xlab="", ylab="", xlim=c(0,100), ylim=c(0,100) ) lines(c(10,90), c(10,90)) lines(c(90,10), c(10,90)) dev.off() system(teenimi2) # Lisanäide - tekstide lisamine Inimesi=c(62175, 65107, 97479, 106728, 98131, 93094, 93792, 88712, 99016, 95566, 91131, 69710, 79954, 69308, 62676, 44707, 22520, 15798) Vanus=c("0-4", "5-9", "10-14", "15-19", "20-24", "25-29", "30-34", "35-39", "40-44", "45-49", "50-54", "55-59", "60-64", "65-69", "70-74", "75-79", "80-84", "85+") plot(1:18,Inimesi, type="l",xlab="", xaxt="n", yaxt="n", ylab="inimeste arv", main="Eesti rahvastiku jaotus vanuse järgi \n 1. jaanuaril 2003" , ylim=c(0, max(Inimesi)) ) axis(side=1, at=1:18, label=Vanus, las=3) axis(side=2, at=c(0,25000,50000,75000,100000,125000), label=c("0","25 000","50 000", "75 000","100 000", "125 000"), las=3) text(12, Inimesi[12]-5000, "II Maailmasõja ajal \n sündimata jäänud lapsed", srt=90, adj=c(1, 0.5)) text(4, Inimesi[4]-5000, "laulev revolutsioon", srt=90, adj=c(1, 0.5))