R画图例子

来源:互联网 发布:螺纹铣刀铣内螺纹编程 编辑:程序博客网 时间:2024/04/30 14:50

Creating a Graph

# Creating a Graphattach(mtcars)plot(wt, mpg)abline(lm(mpg~wt))title("Regression of MPG on Weight")

Simple plot

plot(cars$dist~cars$speed, # y~x     main="Relationship between car distance & speed",     xlab="Speed (miles per hour)", #X axis title     ylab="Distance travelled (miles)", #Y axis title     xlim=c(0,30), #Set x axis limits from 0 to 30     ylim=c(0,140), #Set y axis limits from 0 to 140     xaxs="i", #Set x axis style as internal     yaxs="i", #Set y axis style as internal     col="red", #Set the color of plotting symbol to re     pch=20) #Set the plotting symbol to filled dots
x <- c(1:5); y <- x # create some datapar(pch=22, col="red") # plotting symbol and colorpar(mfrow=c(2,4)) # all plots on one pageopts = c("p","l","o","b","c","s","S","h")for(i in 1:length(opts)){  heading = paste("type=",opts[i])  plot(x, y, type="n", main=heading)  lines(x, y, type=opts[i])}

Dot Plots

# Simple Dotplotdotchart(mtcars$mpg,labels=row.names(mtcars),cex=.7,   main="Gas Milage for Car Models",   xlab="Miles Per Gallon")
# Dotplot: Grouped Sorted and Colored# Sort by mpg, group and color by cylinderx <- mtcars[order(mtcars$mpg),] # sort by mpgx$cyl <- factor(x$cyl) # it must be a factorx$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,   main="Gas Milage for Car Models\ngrouped by cylinder",   xlab="Miles Per Gallon", gcolor="black", color=x$color)

Bar Plots

Simple Bar Plot

# Simple Bar Plotcounts <- table(mtcars$gear)barplot(counts, main="Car Distribution",   xlab="Number of Gears") 
# Simple Horizontal Bar Plot with Added Labelscounts <- table(mtcars$gear)barplot(counts, main="Car Distribution", horiz=TRUE,  names.arg=c("3 Gears", "4 Gears", "5 Gears"))

Stacked Bar Plot

# Stacked Bar Plot with Colors and Legendcounts <- table(mtcars$vs, mtcars$gear)barplot(counts, main="Car Distribution by Gears and VS",  xlab="Number of Gears", col=c("darkblue","red"),  legend = rownames(counts)) 

Grouped Bar Plot

counts <- table(mtcars$vs, mtcars$gear)barplot(counts, main="Car Distribution by Gears and VS",  xlab="Number of Gears", col=c("darkblue","red"),  legend = rownames(counts), beside=TRUE)

Line Charts

x <- c(1:5); y <- x # create some datapar(pch=22, col="red") # plotting symbol and colorpar(mfrow=c(2,4)) # all plots on one pageopts = c("p","l","o","b","c","s","S","h")for(i in 1:length(opts)){  heading = paste("type=",opts[i])  plot(x, y, type="n", main=heading)  lines(x, y, type=opts[i])}
x <- c(1:5); y <- x # create some datapar(pch=22, col="blue") # plotting symbol and colorpar(mfrow=c(2,4)) # all plots on one pageopts = c("p","l","o","b","c","s","S","h")for(i in 1:length(opts){  heading = paste("type=",opts[i])  plot(x, y, main=heading)  lines(x, y, type=opts[i])}
# Create Line Chart# convert factor to numeric for convenienceOrange$Tree <- as.numeric(Orange$Tree)ntrees <- max(Orange$Tree)# get the range for the x and y axisxrange <- range(Orange$age)yrange <- range(Orange$circumference)# set up the plotplot(xrange, yrange, type="n", xlab="Age (days)",   ylab="Circumference (mm)" )colors <- rainbow(ntrees)linetype <- c(1:ntrees)plotchar <- seq(18,18+ntrees,1)# add linesfor (i in 1:ntrees) {  tree <- subset(Orange, Tree==i)  lines(tree$age, tree$circumference, type="b", lwd=1.5,    lty=linetype[i], col=colors[i], pch=plotchar[i])}# add a title and subtitletitle("Tree Growth", "example of line plot")# add a legendlegend(xrange[1], yrange[2], 1:ntrees, cex=0.8, col=colors,   pch=plotchar, lty=linetype, title="Tree")

Pie Charts

simple plot

slices <- c(10, 12,4, 16, 8)lbls <- c("US", "UK", "Australia", "Germany", "France")pie(slices, labels = lbls, main="Pie Chart of Countries")

Pie Chart with Annotated Percentages

# Pie Chart with Percentagesslices <- c(10, 12, 4, 16, 8)lbls <- c("US", "UK", "Australia", "Germany", "France")pct <- round(slices/sum(slices)*100)lbls <- paste(lbls, pct) # add percents to labelslbls <- paste(lbls,"%",sep="") # ad % to labelspie(slices,labels = lbls, col=rainbow(length(lbls)),   main="Pie Chart of Countries") 

3D Pie Chart

# 3D Exploded Pie Chartlibrary(plotrix)slices <- c(10, 12, 4, 16, 8)lbls <- c("US", "UK", "Australia", "Germany", "France")pie3D(slices,labels=lbls,explode=0.1,   main="Pie Chart of Countries ")
  • Creating Annotated Pies from a data frame
# Pie Chart from data frame with Appended Sample Sizesmytable <- table(iris$Species)lbls <- paste(names(mytable), "\n", mytable, sep="")pie(mytable, labels = lbls,   main="Pie Chart of Species\n (with sample sizes)") 

Boxplots

# Boxplot of MPG by Car Cylindersboxplot(mpg~cyl,data=mtcars, main="Car Milage Data",   xlab="Number of Cylinders", ylab="Miles Per Gallon") 
# Notched Boxplot of Tooth Growth Against 2 Crossed Factors# boxes colored for ease of interpretationboxplot(len~supp*dose, data=ToothGrowth, notch=TRUE,  col=(c("gold","darkgreen")),  main="Tooth Growth", xlab="Suppliment and Dose") 
# Violin Plotslibrary(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("Violin Plots of Miles Per Gallon")
# Example of a Bagplotlibrary(aplpack)attach(mtcars)bagplot(wt,mpg, xlab="Car Weight", ylab="Miles Per Gallon",  main="Bagplot Example") 

Scatterplots

Simple Scatterplot

# Simple Scatterplotattach(mtcars)plot(wt, mpg, main="Scatterplot Example",   xlab="Car Weight ", ylab="Miles Per Gallon ", pch=19) 
# Add fit linesabline(lm(mpg~wt), col="red") # regression line (y~x)lines(lowess(wt,mpg), col="blue") # lowess line (x,y) 
# Enhanced Scatterplot of MPG vs. Weight# by Number of Car Cylinderslibrary(car)scatterplot(mpg ~ wt | cyl, data=mtcars,   xlab="Weight of Car", ylab="Miles Per Gallon",   main="Enhanced Scatter Plot",   labels=row.names(mtcars)) 

Scatterplot Matrices

# Basic Scatterplot Matrixpairs(~mpg+disp+drat+wt,data=mtcars,   main="Simple Scatterplot Matrix")
# Scatterplot Matrices from the lattice Packagelibrary(lattice)splom(mtcars[c(1,3,5,6)], groups=cyl, data=mtcars,   panel=panel.superpose,   key=list(title="Three Cylinder Options",   columns=3,   points=list(pch=super.sym$pch[1:3],   col=super.sym$col[1:3]),   text=list(c("4 Cylinder","6 Cylinder","8 Cylinder")))) 
# Scatterplot Matrices from the car Packagelibrary(car)scatterplot.matrix(~mpg+disp+drat+wt|cyl, data=mtcars,   main="Three Cylinder Options")
# Scatterplot Matrices from the glus Packagelibrary(gclus)dta <- mtcars[c(1,3,5,6)] # get datadta.r <- abs(cor(dta)) # get correlationsdta.col <- dmat.color(dta.r) # get colors# reorder variables so those with highest correlation# are closest to the diagonaldta.o <- order.single(dta.r)cpairs(dta, dta.o, panel.colors=dta.col, gap=.5,main="Variables Ordered and Colored by Correlation" )

High Density Scatterplots

# High Density Scatterplot with Binninglibrary(hexbin)x <- rnorm(1000)y <- rnorm(1000)bin<-hexbin(x, y, xbins=50)plot(bin, main="Hexagonal Binning") 
# High Density Scatterplot with Color Transparencypdf("c:/scatterplot.pdf")x <- rnorm(1000)y <- rnorm(1000)plot(x,y, main="PDF Scatterplot Example", col=rgb(0,100,0,50,maxColorValue=255), pch=16)dev.off()

3D Scatterplots

# 3D Scatterplotlibrary(scatterplot3d)attach(mtcars)scatterplot3d(wt,disp,mpg, main="3D Scatterplot")
# 3D Scatterplot with Coloring and Vertical Drop Lineslibrary(scatterplot3d)attach(mtcars)scatterplot3d(wt,disp,mpg, pch=16, highlight.3d=TRUE,  type="h", main="3D Scatterplot")
# 3D Scatterplot with Coloring and Vertical Lines# and Regression Planelibrary(scatterplot3d)attach(mtcars)s3d <-scatterplot3d(wt,disp,mpg, pch=16, highlight.3d=TRUE,  type="h", main="3D Scatterplot")fit <- lm(mpg ~ wt+disp)s3d$plane3d(fit)
# Spinning 3d Scatterplotlibrary(rgl)plot3d(wt, disp, mpg, col="red", size=3) 
# Another Spinning 3d Scatterplotlibrary(Rcmdr)attach(mtcars)scatter3d(wt, disp, mpg) 

Density Plots

Histograms

# Simple Histogramhist(mtcars$mpg)# Colored Histogram with Different Number of Binshist(mtcars$mpg, breaks=12, col="red") # Add a Normal Curve x <- mtcars$mpgh<-hist(x, breaks=10, col="red", xlab="Miles Per Gallon",   main="Histogram with Normal Curve")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) 

Kernel Density Plots

# Kernel Density Plotd <- density(mtcars$mpg) # returns the density dataplot(d) # plots the results # Filled Density Plotd <- density(mtcars$mpg)plot(d, main="Kernel Density of Miles Per Gallon")polygon(d, col="red", border="blue") 

Comparing Groups VIA Kernal Density

# Compare MPG distributions for cars with# 4,6, or 8 cylinderslibrary(sm)attach(mtcars)# create value labelscyl.f <- factor(cyl, levels= c(4,6,8),  labels = c("4 cylinder", "6 cylinder", "8 cylinder"))# plot densitiessm.density.compare(mpg, cyl, xlab="Miles Per Gallon")title(main="MPG Distribution by Car Cylinders")# add legend via mouse clickcolfill<-c(2:(2+length(levels(cyl.f))))legend(locator(1), levels(cyl.f), fill=colfill) 

0 0
原创粉丝点击