#3#### attach(mtcars) plot(wt,mpg) abline(lm(mpg~wt)) title('Regression of MPG on Weight') detach(mtcars) pdf('mygraph.pdf') attach(mtcars) plot(wt,mpg) abline(lm(mpg~wt)) title('Regression of MPG on Weight') detach(mtcars) dev.off() dose <- c(20,30,40,45,60) drugA <- c(16,20,27,40,60) drugB <- c(15,18,25,31,40) plot(dose,drugA,type = 'b') par() #查看当前的所有图形参数/ opar <- par(no.readonly = TRUE) #默认设置 par(lty=2,pch=17) plot(dose,drugA,type = 'b') par(opar) #还原默认设置 plot(dose,drugA,type = 'b',lty=3,pch=13) plot(dose,drugA,type = 'b',lty=3, lwd=3, cex=3, pch=21, col='blue', fg='red', bg='black', col.lab='yellow', col.axis='green', ) # col /col.axis/col.lab/col.main/col.sub/fg/bg # cex /cex.axis/cex.lab/cex.main/cex.sub # font /font.axis/font.lab/font.main/font.sub/ps/family par(pin=c(4,3),mai=c(1,.5,1,.2)) #size of picture plot(dose,drugA,type = 'b', col='red',lty=2,pch=2,lwd=2, main='clinicacl trials for drug A', sub='this is hypothetical data', xlab = 'dosage',ylab = 'drug response', xlim = c(0,60),ylim = c(0,70)) title(main = "my title",col.main='red', sub="my subtitle",col.sub='blue', xlab = 'my x lable',ylab = 'my y lable', col.lab = 'green', cex.lab = '0.75') x <- c(1:10) y <- x z <- 10/x opar <- par(no.readonly = TRUE) par(mar=c(5,4,4,8) + 0.1) plot(x,y,type = 'b', pch=23,col='red', yaxt='n',xaxt='n',ann = False) lines(x,z,type = 'b', pch=22,col='blue',lty=2) axis(2,at=x,labels = x,col.axis='red',fg='grey') axis(4,at=z,labels = round(z,digits = 2),las=2,cex.axis=0.7,tck=-0.01) mtext('y=1/x',side = 4,line = 3,cex.lab=3,las=2,col='blue') title("an example of creative axes",xlab = 'x value',ylab = 'y value') par(opar) abline(h=c(1,5,7)) abline(v=seq(1,10,2),lty=2,col='blue') dose <- c(20,30,40,45,60) drugA <- c(16,20,27,40,60) drugB <- c(15,18,25,31,40) opar <- par(no.readonly = TRUE) par(lwd = 2, cex = 1.5, font.lab = 1) plot(dose, drugA, type = 'b', pch =15, lty = 1, col = 'red', ylim = c(0,60), main = 'drug A vs. drug B', xlab = 'drug dosage', ylab = 'drug response') lines(dose,drugB, type = 'b', pch=17, lty=2,col='blue') abline(h=c(30),lwd=1.5,lty=2,col='gray') library(Hmisc) minor.tick(nx=4,ny=4,tick.ratio = 0.5) legend('left', inset = .05,title='drug type',c('a','b'), lty=c(1,2),pch = c(15,17),col=c('red','blue')) par(opar) attach(mtcars) plot(wt,mpg, main = 'mileage vs. car weight', xlab = 'weight',ylab = 'mileage', pch=18,col='blue') text(wt,mpg, row.names(mtcars), cex = 0.6,pos = 4,col = 'red') detach(mtcars) head(mtcars) opar <- par(no.readonly = TRUE) par(cex=1.5) plot(1:7,1:7,type = 'n') text(3,3,'example of default text') text(4,4,family='mono','example of mono-spaced text') attach(mtcars) opar <- par(no.readonly = TRUE) par(mfrow=c(2,2)) plot(wt,mpg,main = 'scatterplot of wt vs. mpg') plot(wt,disp,main='scatterplot of wt vs. disp') hist(wt, main='hisogram of wt') boxplot(wt,main='boxplot of wt') par(opar) detach(mtcars) attach(mtcars) rm(list = ls()) opar <- par(no.readonly = TRUE) par(mfrow=c(3,1)) hist(wt) hist(mpg) hist(disp) par(opar) detach(mtcars) attach(mtcars) layout(matrix(c(1,1,2,3),2,2,byrow = TRUE)) hist(wt) hist(mpg) hist(disp) detach(mtcars) attach(mtcars) layout(matrix(c(1,1,2,3),2,2,byrow = TRUE), widths = c(3,1),heights = c(1,2)) #? hist(wt) hist(mpg) hist(disp) detach(mtcars) opar <- par(no.readonly = TRUE) par(fig=c(0,0.8,0,0.8)) plot(mtcars$wt,mtcars$mpg, xlab = 'miles per gallon', ylab = 'car weight') par(fig=c(0,0.8,0.55,1),new=TRUE) boxplot(mtcars$wt,horizontal = TRUE,axes=FALSE) par(fig=c(0.65,1,0,0.8),new=TRUE) boxplot(mtcars$mpg,axes=FALSE) mtext('enhanced scatterplt',side = 3,outer = TRUE,line = -3) par(opar) #5#### library(vcd) counts <- table(Arthritis$Improved) counts barplot(counts,main = 'simple bar plot', xlab = 'improvement',ylab = 'frequency') barplot(counts,main = 'horizontal bar plot', xlab = 'frequency',ylab = 'improvement', horiz = TRUE) library(vcd) counts <- table(Arthritis$Improved,Arthritis$Treatment) counts barplot(counts,main = 'stacked bar plot', #stacked xlab = 'treatment',ylab = 'frequency', col=c('red','yellow','green'), legend.text = rownames(counts)) barplot(counts,main='grouped bar plot', xlab = 'treatment',ylab = 'frequency', col = c('red','green','yellow'), legend.text = rownames(counts),beside = TRUE) par(opar) states <- data.frame(state.region,state.x77) means <- aggregate(states$Illiteracy,by=list(state.region),FUN=mean) means <- means[order(means$x),] means barplot(means$x, names.arg = means$Group.1) title('mean Illiteracy Rate') par(mar = c(5,8,4,2)) par(las=2) counts <- table(Arthritis$Improved) barplot(counts, main = 'treatment outcome', horiz = TRUE,cex.names = 0.8, names.arg = c('no improvement','some improvement', 'marked improvment')) library(vcd) attach(Arthritis) counts <- table(Treatment, Improved) spine(counts,main = 'spingram example') detach(Arthritis) par(mfrow=c(2,2)) slices <- c(10,12,4,16,8) lbls <- c('US','uk','ustralia','germany','france') pie(slices,labels = lbls, main = 'simple pie chart') pct <- round(slices/sum(slices)*100) lbls2 <- paste(lbls,' ',pct, '%',sep = '') pie(slices,labels = lbls2,col = rainbow(length(lbls2)), main = 'pie chart with percentages') library(plotrix) pie3D(slices,labels = lbls,explode=0.1, main='3D pie chart') mytable <- table(state.region) lbls3 <- paste(names(mytable),'\n',mytable,sep = ' ') pie(mytable,labels = lbls3, main = 'pie chart from a table\n (with sample sizes)') library(plotrix) slices <- c(10,12,4,16,8) lbls <- c('us','uk','australia','germany','france') fan.plot(slices,labels = lbls,main='fan plot') par(mfrow=c(2,2)) hist(mtcars$mpg) hist(mtcars$mpg,breaks = 12, col='red', xlab = 'milees per gallon', main = 'colored histogram with 12 bins') hist(mtcars$mpg,freq = FALSE, breaks=12, col = 'red', xlab = 'miles per gallon', main = 'histogram, rug plot density curve') rug(jitter(mtcars$mpg)) lines(density(mtcars$mpg),col='blue',lwd=2) x <- mtcars$mpg h <- hist(x, breaks=12,col = 'red', xlab = 'miles per gallon', main = 'histogram with moral curve and box') xfit <- seq(min(x),max(x),length(40)) yfit <- dnorm(xfit,mean = mean(x),sd=sd(x)) yfit <- yfit*diff(h$mids[1:2])*length(x) lines(xfit,yfit,col='blue',lwd=2) box() par(opar) par(mfrow=c(2,1)) d <- density(mtcars$mpg) plot(d) plot(d,main = 'kernel density of miles per gallon') polygon(d,col='red',borders='blue') rug(mtcars$mpg,col='brown') library(sm) attach(mtcars) cyl.f <- factor(cyl,levels = c(4,6,8), labels = c('4 cylinder','6 cylinder', '8 cylinder')) sm.density.compare(mpg,cyl,xlab='miles per gallon') title(main = 'MPG distribution by car cylinders') colfill<- c(2:(1+length(levels(cyl.f)))) legend(locator(1),levels(cyl.f),fill = colfill) detach(mtcars) boxplot(mtcars$mpg,main= 'box plot',ylab='miles per gallon') View(mtcars) boxplot(mpg ~ carb , data = mtcars, main='car mileage data', xlab = 'number of cylinders', ylab = 'miles per gallon') boxplot(mpg ~ cyl, notch=TRUE, varwidth=TRUE, col='red', data = mtcars, main='car mileage data', xlab = 'number of cylinders', ylab = 'miles per gallon') mtcars$cyl.f <- factor(mtcars$cyl, levels = c(4,6,8), labels = c('4','6','8')) mtcars$am.f <- factor(mtcars$am, levels = c(0,1), labels = c('auto','standard')) boxplot(mpg~am.f*cyl.f, data=mtcars, varwidth=TRUE, col=c('gold','darkgreen'), main='mpg disrtibution by auto type', xlab = 'auto type',ylab = 'miles per gallon') library(vioplot) x1 <- mtcars$mpg[mtcars$cyl==4] x2 <- mtcars$mpg[mtcars$cyl==6] x3 <- mtcars$mpg[mtcars$cyl==8] vioplot(x1,x2,x3, names = c('4 cyl','6 cyl','8 cyl'), col = 'gold') title('vioin plots of miles per gallon', ylab = 'miles per galllon', xlab = 'number of cylinders') dotchart(mtcars$mpg,labels = row.names(mtcars),cex = .7, main = 'gas mileage for car models', xlab = 'mile per gallon') x <- mtcars[order(mtcars$mpg),] x$cyl <- factor(x$cyl) x$color[x$cyl==4] <- 'red' x$color[x$cyl==6] <- 'blue' x$color[x$cyl==8] <- 'darkgreen' dotchart(x$mpg, labels = row.names(x), cex=.7, groups = x$cyl, gcolor = 'black', color = x$color, pch=19, main = 'gas mileage for car models\ngrouped by cylinder', xlab = 'miles per gallon') attach(mtcars) plot(wt,mpg, main = 'basic scatter plot of MPG vc. weight', xlab = 'car weight (1bs/1000)', ylab = 'mile per gallon', pch=19) abline(lm(mpg~wt,col='red',lwd=2,lty=2)) lines(lowess(wt,mpg),col='blue',lwd=2,lty=2) library(car) scatterplot(mpg~wt|cyl,data=mtcars,lwd=2,span=0.75, main='scatter plot of MPG vs. weight by # cylinder', xlab = 'weight of car (lbs/1000)', ylab = 'mile per gallon', legend.plot=TRUE, id.method='identify', labels = row.names(mtcars), boxplots='xy' ) pairs(~mpg+disp+drat+wt,data = mtcars, main='basic scatter plot matrix') plot(mpg,disp) plot(mpg,drat) library(car) scatterplotMatrix(~mpg+disp+drat+wt,data=mtcars, spread=FALSE, smoother.args=list(lty=2), main='scatter plot matrix via car package') set.seed(1234) n <- 1000 c1 <- matrix(rnorm(n,mean = 0,sd=0.5),ncol = 2) c2 <- matrix(rnorm(n,mean = 3,sd=2),ncol = 2) mydata <- rbind(c1,c2) mydata <- data.frame(mydata) names(mydata) <- c('x','y') with(mydata,plot(x,y,pch=19,main='scatter plot with 10,000 observations')) with(mydata,smoothScatter(x,y,main='scatter plot colored by smoothed densities')) library(hexbin) with(mydata,{ bin <- hexbin(x,y,xbins=50) plot(bin,main='hexagonal binning with 10,000 observations') }) library(scatterplot3d) attach(mtcars) scatterplot3d(wt,disp,mpg, main = 'basic 3d scatter plot') detach(mtcars) library(scatterplot3d) attach(mtcars) s3d <- scatterplot3d(wt,disp,mpg, pch=16, highlight.3d= T, type="h", main='3d scatter plot with vertical lines') fit <- lm(mpg~wt+disp) s3d$plane3d(fit) library(rgl) attach(mtcars) plot3d(wt,disp,mpg,col='red',size = 5) ##交互 detach(mtcars) library(car) with(mtcars, scatter3d(wt,disp,mpg)) attach(mtcars) r <- sqrt(disp/pi) symbols(wt,mpg,circles = r,inches=0.3, fg='white',bg='lightblue', main = 'bubble plot with point size proportional to displacement', ylab = 'mile per gallon', xlab = 'weight of car (lbs/1000)') text(wt,mpg,rownames(mtcars),cex = 0.6) detach(mtcars) opar <- par(no.readonly = T) par(mfrow=c(1,2)) t1 <- subset(Orange,Tree==1) plot(t1$age,t1$circumference, xlab='Age(days', ylab='cricumstance(mm)', main='orange tree 1 growth') plot(t1$age,t1$circumference, xlab='age (days)', ylab='cricumstance (mm)', main='orange tree 1 growth', type='b') par(opar) plot(t1$age,t1$circumference,type='n') class(Orange$Tree) rm(list = ls()) Orange$Tree <- as.numeric(Orange$Tree) ntrees <- max(Orange$Tree) xrange <- range(Orange$age) yrange <- range(Orange$circumference) plot(xrange,yrange,type='n', xlab='Age (days)', ylab='circumstance (mm)') colors <- rainbow(ntrees) linetype <- c(1:ntrees) plotchar <- seq(18,18+ntrees,1) for (i in 1:ntrees){ tree <- subset(Orange,Tree==i) lines(tree$age,tree$circumference, type = 'b', lwd=2, lty=linetype[i], col=colors[i], pch=plotchar[i] ) } title('Tree growth','example of line plot') legend(xrange[1],yrange[2], 1:ntrees, cex = 0.8, col=colors, pch = plotchar,lty = linetype, title = 'Tree') options(digits = 2) cor(mtcars) library(corrgram) corrgram(mtcars,order = TRUE,lower.panel = panel.shade, upper.panel = panel.pie,text.panel = panel.txt, main='corrgram of mtcars intercorrelations') corrgram(mtcars,order=TRUE,lower.panel = panel.ellipse, upper.panel = panel.pts,text.panel = panel.txt, diag.panel = panel.minmax, main='corrgram of mtcars data using scatter plots anf ellipses') corrgram(mtcars,order=TRUE,lower.panel = panel.shade, upper.panel = NULL, text.panel = panel.txt, main='car milieage data(unsorted)') cols <- colorRampPalette(c('darkgoldenrod4','burlywood1','darkkhaki','darkgreen')) corrgram(mtcars,order = TRUE,col.regions = cols, lower.panel = panel.shade, upper.panel = panel.conf, text.panel = panel.txt, main='A CORRGRAM (or hourse) of a different color') ftable(Titanic) library(vcd) mosaic(~Class+Sex+Age+Survived,data = Titanic,shade=TRUE,legend=TRUE)