#################################################################################################################### ###################### Scatter Plots - symbols, lines, legends ################################### #################################################################################################################### data(Puromycin) attach(Puromycin) #### DATA DESCRIPTION help(Puromycin) # Rate represents the instantaneous reaction rates(counts/min/min) # Conc repressents the substrate concentrations (ppm) # State represents the cell treatment (Puromycin - antibiotic inhibiting translation) ### SIMPLE HISTOGRAM OF REACTION RATE hist(rate,breaks=20,freq=FALSE) lines(density(rate),col="red",lwd=2) ### INDEX PLOT plot(rate) ### TWO-VARIABLES PLOT plot(rate~conc,data=Puromycin) ### CHANGE THE LIMITS OF THE AXIS plot(rate~conc,data=Puromycin,xlim=c(0,1.2),ylim=c(45,210)) ### CHANGE THE LABELS AND THE FONT SIZE plot(rate~conc,xlim=c(0,1.2),ylim=c(45,210),xlab="Concentration",ylab="Rate",cex.lab=1.2) ### CHANGE BACKGROUND par(bg="lightcyan") plot(rate~conc,xlim=c(0,1.2),ylim=c(45,210),xlab="Concentration",ylab="Rate",cex.lab=1.2) ### ADD A REFERENCE LINE ### Let's add the regression line reg<-lm(rate~conc) abline(reg, lwd=2) ### CHANGE COLORS - Red for Treated, Black for Untreated. ### You can use numbers or you can use words, but for the latter, make sure you use " ". names(Puromycin) plot(rate~conc,xlim=c(0,1.2),ylim=c(45,210),xlab="Concentration",ylab="Rate",cex.lab=1.2, col=ifelse(state=="treated","red","black"),cex=c(2,2)) ### CHANGE THE PLOTTING SYMBOL - pch(plottting character) plot(rate~conc,xlim=c(0,1.2),ylim=c(45,210),xlab="Concentration",ylab="Rate",cex.lab=1.2, col=ifelse(state=="treated","green3","hotpink"),cex=c(1.5,1.5),pch=19) ### pch=21 empty circle, BG sets the inside, COL sets the border plot(rate~conc,xlim=c(0,1.2),ylim=c(45,210),xlab="Concentration",ylab="Rate",cex.lab=1.2, bg=ifelse(state=="treated","royalblue","orangered"), cex=c(1.5,1.5),pch=21,col=c(1,1)) ### ADD TEST INSIDE THE GRAPH text(1.1, 152, label = "important point", pos = 1) ### CREATE TWO DATA SETS: Treated and Untreated PuroT <- subset(Puromycin, state == "treated") PuroU <- subset(Puromycin, state == "untreated") ### ADD SMOOTHING LINES smooth <- with(PuroT, lowess(rate ~ conc)) lines(lowess(rate~conc), lty=1, lwd=2, col="royalblue") smooth <- with(PuroU, lowess(rate ~ conc)) lines(smooth, lty=1, lwd=2, col="orangered") ### HORIZONTAL REFERENCE LINE abline(h=200,lty=2) ### ADD TITLE AND LEGEND title(main="Puromycin Data", cex.main=1.5) legend(x=0.8, y=100, legend=c("Treated", "Untreated"), pch=c(19,19), col=c("royalblue","orangered"), lty=c(1,1), lwd=c(2,2), cex=c(1.2,1.2)) ### REMOVE THE BOX AROUNND THE LEGEND -> bty="n" legend("bottomright", legend=c("Treated", "Untreated"), pch=c(19,19), col=c("royalblue","orangered"),lty=c(1,1), lwd=c(2,2),cex=c(1.2,1.2), bty="n") ### CREATE MULTIPLE GRAPHS IN THE SAME WINDOW par(mfrow=c(1,2)) ### CHANGE THE MARGINS TO REMOVE EXTRA SPACE #par(mar=c(4, 4, 0.5, 0.5)) ### CHANGE THE OUTER MARGINS #par(oma=c(0,0,3,0)) plot(rate~conc, data=PuroT, pch=21, bg="royalblue", col=1, xlab="Concentration", ylab="Rate", cex.lab=1.2) plot(rate~conc, data=PuroU, pch=21, bg="orangered", col=1, xlab="Concentration", ylab="Rate", cex.lab=1.2) ### ADD A "COMMON" TITLE mtext("Puromycin Data", side=3, adj=5, line=1.5, font=2, cex=2, col="red", outer=F) ### SAME THING USING XYPLOT() # Separate scatterplots for each level of the factor library(lattice) xyplot(rate ~ conc | state, type = c("p", "smooth"), span=2, pch=19, col=2, main = "Puromycin", lwd=2, xlab="Concentration", ylab="Rate", cex.lab=1.2) ### Let's try par() wiht a regression object reg<-lm(rate~conc) par(mfrow=c(2,2)) ### Change the margins of the plots #par(mar=c(4, 4, 2, 1)) plot(reg) ############################################################################################################ ###################### Scatterplot Matrix ################################################## ############################################################################################################ plot(Puromycin) ### SAME THING WITH PAIRS() pairs(Puromycin) ### ADD A SMOOTHING LINE pairs(Puromycin, panel = panel.smooth,lwd=2) ############################################################################################################ #################### Multivariable Graph ################################################# ################# Individual Profiles for 97 subjects ################################################ ### Prostate Data data<-read.csv('F://PhD Program_MUSC//R Graphics//Prostate.csv',header=T) dim(data) data[1:10,] ### Standardize Predictors with scale() pred<-scale(data[,1:9],center=T,scale=T) lcavol.st<-pred[,2] lweight.st<-pred[,3] age.st<-pred[,4] lbhp.st<-pred[,5] lcp.st<-pred[,7] ### DICHOTOMIZE PSA: N < 10 and H >= 10 psa<-exp(data[,10]) new.psa <- ifelse(psa >= 10, 1, 2) new.data<-cbind(lcavol.st,lweight.st,age.st,lbhp.st,lcp.st,new.psa) ### CREATE TWO DATA SETS BY < 10 AND 10+ LEVELS OF PSA new.dataN<-new.data[1:41,] new.dataH<-new.data[42:97,] ### JITTER THE POINTS ON THE VERTICAL n1<-ncol(new.dataN)-1 n2<-ncol(new.dataH)-1 noise1<-runif(n1,-.15,.15) # BLUE noise2<-runif(n2,-.15,.15) # RED ### PLOT DATA BY PSA < 10 plot(1:5,new.dataN[1,1:5] + noise1, type="b", pch=19, col="#0000FF32", cex=1.25, xaxt="n", xlab="", ylab="Standardized value", cex.lab=1.25, cex.axis=1.25, ylim=c(-3.1,2.7), lwd=2) axis(side=1,at=c(1,2,3,4,5),labels=c("log CA vol","log weight","Age","log BHP","log CP"),cex.axis=1.25) for (i in 2:41){ lines(1:5, new.dataN[i,1:5]+ noise1, type="b", col="#0000FF32", cex=1.25, lwd=2, pch=19) } ### PLOT DATA BY THE PSA VALUES 10+ for (i in 1:56){ lines(1:5, new.dataH[i,1:5]+noise2, type="b", col="#FF000032", cex=1.25, lwd=2, pch=19) } legend("bottomright", legend=c("PSA < 10","PSA = 10+"), pch=19, col=c("#0000FF32","#FF000032"), lty=c(1,1), cex=1.25, bty="n", lwd=2)