1. 条形图 barplot()
#载入vcd包 library(vcd) #table函数提取各个维度计数 counts <- table(Arthritis$Improved) counts #绘制简单Improved条形图 #1行2列 par(mfrow=c(1,2)) barplot(counts, main = "simple Bar plot", xlab = "Improved",ylab = "Frequency") #绘制水平条形图 horiz = TRUE barplot(counts,main = "simple Bar plot", xlab = "Frequency",ylab = "Improved",horiz=TRUE) #如果绘制的是一个有序因子,可以使用plot()函数快速创建一幅垂直条形图 #1行1列 par(mfrow=c(1,1)) plot(Arthritis$Improved,xlab = "Frequency",ylab = "Improved",horiz=TRUE) #如果要绘制的变量是一个矩阵而不是一个向量,将会绘制堆砌条形图或者分组条形图 #生成Improved和Treatment列联表 counts <- table(Arthritis$Improved,Arthritis$Treatment) counts #绘制堆砌图 barplot(counts,main = "Stacked Bar plot",xlab = "Treatment",ylab = "Frequency",col = c(\'red\',\'yellow\',\'green\'),legend = rownames(counts)) #绘制分组条形图 barplot(counts,main = "Grouped Bar plot",xlab = "Treatment",ylab = "Frequency",col = c(\'red\',\'yellow\',\'green\'),legend = rownames(counts),beside = TRUE) #条形图微调 #增加y边界大小 par(mar = c(5,8,4,2)) #las=2旋转条形标签 par(las = 2) counts <- table(Arthritis$Improved) #cex.names= 0.8缩小字体的大小 barplot(counts,main="Treatment Outcomes",horiz = TRUE,cex.names=0.8,names.arg = c("No Improvement","Some Improvement","Marked Imporvement"))
2. 饼图:饼图在商业世界中无所不在,然而多数统计学家,包括R相应文档的编写者,都对它持否定态度。相对于饼图,他们更推荐使用条形图或点图,因为相对于
面积,人们对长度的判断更为精确 pie() pie3D()
par(mfrow=c(2,2)) slices <- c(10,12,12.4,16,8) lbls <- c("US","UK","Austrialia","Germany","France") pie(slices,labels=lbls,main="simple Chart") pct <- round(slices/sum(slices)*100) #拼接字符串和比例数值 lbls2 <- paste(lbls," ", pct,"%",sep = "") lbls2 pie(slices,labels=lbls2,col=rainbow(length(lbls2)),main="Pie chart with percentage") #载入ploirix包 library(plotrix) #画简单3D图 pie3D(slices,labels=lbls,explode = 0.4,main = "3D pei chart") #从表格创建饼图 #table取得region的计数表 mytable <- table(state.region) #names 函数取得列名,然后将列名和相应的计数拼接在一起 lbls3 <- paste(names(mytable),\'\n\',mytable,sep="") pie(mytable,labels=lbls3,main="pie chart from a table\n{with sample sizes}")
3. 直方图 :可以展示连续变量的分布. hist(x,breaks=, freq = )
x是一个由数据值组成的数值向量,参数freq = FALSE表示根据概率密度而不是频数绘制图形,参数breaks用于控制数组的数量.
par(mfrow = c(2,2)) #简单直方图 hist(mtcars$mpg) #指定组数和颜色 hist(mtcars$mpg,breaks=12,col=\'red\',xlab="Miles 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 normal 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()
4. 核密度图:核密度估计是用于估计随机变量概率密度函数的非参数方法
#当前图像参数列表 opar <- par(no.readonly = TRUE) par(mfrow=c(2,1)) #默认条件创建 d <- density(mtcars$mpg) plot(d) d <- density(mtcars$mpg) #添加标题和曲线 plot(d,main="kernel Density of Miles per Gallon") polygon(d,col=\'red\',border="blue") #添加棕色轴须图 rug(mtcars$mpg,col="brown") #可比较的核密度函数 #线宽为双倍 #还原初始设置 par(opar) par(lwd=2) library(sm) attach(mtcars) sm.density.compare(mpg,cyl,xlab=\'Miles per gallon\') cyl.f <- factor(cyl,levels=c(4,6,8),labels=c("4 cylinder","6 cylinder","8 cylinder")) 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)
5. 箱线图:boxplot()
boxplot(mtcars$mpg,main="Box plot",ylab="Miles per gallon") #并列箱线图跨列比较 boxplot(mpg~cyl,data=mtcars,main="Car Mileage Data",xlab="Number of Cylinders",ylab="Miles per gallon") #图中可以看到四缸,六缸,八缸耗油中位数不同
6. 点图:在简单水平刻度上绘制大量有标签值的方法, dotchart()
dotchart(mtcars$mpg,labels=row.names(mtcars),cex=.7,main="Gas Mileage for Car Models",xlab="Miles 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]<-\'dark green\' dotchart(x$mpg,labels=row.names(x),cex=.7,group=x$cyl,gcolor="black",color=x$color,pch=19, main="Gas Mileage for Car models\n grouped by cylinder",xlab=\'Miles Per gallon\')
请发表评论