# Chapter 11 # Demonstrations of some R graphics demo(graphics) demo(image) #Creating and managing windows windows() #or quartz() on a Mac or dev.new() in general windows() windows() dev.set() #cycle through the open windows, including RStudioGD dev.set() dev.set() dev.set(3) #May need this to advance to open device dev.set(2) dev.off() #A simple figure zvars=rnorm(100) plot(zvars) #Plotting argument defaults par() # Section 11.1 Single variable graphics data(state) state.dfr = data.frame(I(state.abb),state.region,state.x77) attach(state.dfr) hist(Population) hist(Population,main="Population in 1,000s by State, 1977") plot(density(Population)) load("July_15_COVID.RData") attach(July_15_COVID) hist(Active,main="Active COVID-19 Cases (July 15, 2020)") # Printing in separate windows hist(Population) quartz() dev.set(4) hist(Population,main="Population in 1,000s by State, 1977") dev.set(2) #Plot histogram on density scale so we can overly density estimate hist(Population,main="Population in 1,000s by State, 1977",freq=FALSE) PopDensity=density(Population) lines(PopDensity$x,PopDensity$y) # 11.2 Two variable graphics plot(Illiteracy,Murder) plot(Deaths,Confirmed) plot(Deaths,Confirmed,log="xy") #log-log plot #las=1 prints axis labels horizontally plot(Illiteracy,Murder,las=1,lwd=2,cex=1.2,pch=19,xlab="Percent Illiterate",ylab="") title("Murders per 100,000 vs. Percent Illiterate") text(2,4,"The 50 States",cex=1.2, adj=0, col=2) # Function plotting Illiteracy2=Illiteracy^2 lm(Murder~Illiteracy+Illiteracy2) Illgrid = pretty(Illiteracy,50) Illgrid Murderhat = 1.6627+5.5642*Illgrid-0.4586*Illgrid^2 lines(Illgrid,Murderhat,lty=2,lwd=2,col=2) text(0.5,14,"With Fitted Quadratic", adj=0, col=2) #locator(1) is a good substitute for use of a text command--left justified text(locator(1),"With Fitted Quadratic", adj=0, col=2) colors() curve(dnorm,-4,4) curve(dnorm,-4,4,ylab="Standard Normal Density") curve(dnorm,-4,4,main="Standard Normal Density",ylab=expression(phi(x))) # 11.3 Multiple plots on a page # A simple example par(mfrow=c(1,2)) plot(Illiteracy,Murder) plot(Frost,Illiteracy) par(mfrow=c(1,1)) #trellis graphs library(lattice) xyplot(Murder~Illiteracy|state.region) plot(Hospitalization_Rate,Deaths) #Is gap at x=8 due to region? xyplot(Deaths~Hospitalization_Rate|Region) xyplot(Recovered~Active,scales=list(x=list(log=T),y=list(log=T))) xyplot(Recovered~Active|Region,scales=list(x=list(log=T),y=list(log=T))) xyplot(Recovered~Active,scales=list(x=list(log=T),y=list(log=T)),col=Region) # Demonstrating plotting characters and line types par(mfrow=c(1,2),pty="s") plot(1:25,1:25,type="n") for (i in 1:25) points(i,i,pch=i,cex=0.7) title("25 pch symbols") plot(1:6,1:6,type="n") for (i in 1:6) lines(c(1,6),c(i,i),lty=i,lwd=2) title("6 lty line types") #An improved display for the plotting characters par(mfrow=c(1,1)) plot(c(0.5,5.5),c(0.5,5.5),type="n",xlab="",ylab="",main="Plotting symbols 1-25, row by row") rowseq=rep(1:5,each=5) colseq=rep(1:5,5) pchval=(colseq-1)*5+rowseq points(rowseq,colseq,pch=pchval,cex=2.0) text(rowseq,colseq-.4,paste(pchval),cex=0.7) # 11.4 3-D plots library(help="datasets") data(volcano) dim(volcano) head(volcano) contour(volcano) #colors needs an argument filled.contour(volcano,col=terrain.colors(10)) filled.contour(volcano,col=terrain.colors(25)) persp(volcano) persp(volcano,theta=45) persp(volcano,theta=45,phi=45) #These last two do not work well persp(volcano,theta=45,phi=90) persp(volcano,theta=135,phi=45) image(volcano) image(volcano,col=terrain.colors(10)) #11.5 Interactive graphics # Highlighting and saving plotting points plot(Illiteracy,Murder) mypoints=identify(Illiteracy,Murder,state.abb) mypoints state.abb[mypoints] #Scatterplot matrices pairs(July_15_COVID[,2:11],log="xy",cex=0.3) #Click Zoom for a better look #Chapter 12 Function Writing #Reading in a function fix(twosamp.myfct) source("TwoSampwName.txt") help(table) #past twosamp.txt into window after fix() command fix(twosamp.fct) twosamp.myfct=edit() #Executing a function data(sleep) help(sleep) twosamp.myfct(sleep$extra,sleep$group) source("TwoSampwName1.txt") #Using t.test to analyze data two different ways plot(extra~group,data=sleep) t.test(extra~group,data=sleep) with(sleep,t.test(extra[group==1],extra[group==2])) #Experiment with browser() fix(twosamp.myfct) #Place browser() after ybar statement twosamp.myfct(sleep$extra,sleep$group) ybar varvec nvec n n ybar varvec nvec n ybar varvec nvec Q #or use c for normal execution