1、条形图
options(repos=structure(c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")))
#6.1条形图
#(1)对向量数据绘图
library(vcd)
dt<-Arthritis
count<-table(dt$Improved)#对表中的数据进行计数
count
class(count)
barplot(count,main='Simple bar chart',xlab = "improvemenr",ylab="frequency")#条形图图
#横着画
barplot(count,main='Simple bar chart',xlab = "improvemenr",ylab="frequency",horiz = TRUE)
#(2)对矩阵数据绘图
count1<-table(dt$Improved,dt$Treatment)
count1
#堆砌状
barplot(count1,col=c("red","green","yellow"))
#分组
barplot(count1,col=c("red","green","yellow"),beside=TRUE)
#(3)对table进行绘图:均值条形图(对统计结果进行画图)
states<-data.frame(state.region,state.x77)
#分区域统计文盲率
means<-aggregate(sates$Illiteracy,by=list(state.region),FUN=mean)
#排序
means<-means[order(means$x),]
class(means)
barplot(means$x,names.arg = means$Group.1)
#棘状图
counts<-table(dt$Treatment,dt$Improved)
counts
spine(counts,main="spinogram example")
2、饼图
#6.2饼图 语法:pie(x,label)
x<-c(1,2,3)
lbls<-c("us","jp","ch")
pie(x,labels = lbls,main='simple pie chart',radius = 0.5)
#显示份额
pct<-round(x/sum(x)*100)
pct
lbls2<-paste(lbls," ",pct,"%",sep = "")
pie(x,labels = lbls2,main='simple pie chart',radius = 1.3)
#扇形图
x<-c(1,2,3)
lbls<-c("us","jp","ch")
install.packages("plotrix")
library(plotrix)
fan.plot(x,labels=lbls)
3、直方图
#6.3直方图
dt<-mtcars
hist(dt$mpg)
hist(dt$mpg,breaks = 12,col="red")#分成12组,颜色是红色
hist(dt$mpg,breaks = 12,freq=FALSE)#使得纵轴为百分比
rug(jitter(dt$mpg))#轴须图
lines(density(dt$mpg),col="blue",lwd=2)#加上概率分布的密度图
box()#加上框
4、核密度图
#6.4核密度图
#6.4.1用来估计随机变量的图形形态
d<-density(dt$mpg)
plot(d,main='title')
polygon(d,col="red",border = "blue")
rug(dt$mpg,col="brown")
#6.4.2可比较的核密度图
#比较概率密度是否有明显的差别
install.packages("sm")
library(sm)
attach(mtcars)
#创建分组变量
cyl.f<-factor(cyl,levels = c(4,6,8),labels = c("4 l","6 l","8 l"))
cyl.f
View(cyl.f)
sm.density.compare(mpg,cyl)#cyl不同时mpg的核密度图
title(main = "mpg distribution by car cylinder")
colfill<-c(2:(1+length(levels(cyl.f))))#需要多少颜色
legend(locator(1),levels(cyl.f),fill=colfill)
detach(mtcars)
5、箱线图
#6.5箱线图
#6.5.1箱线图
boxplot(mtcars$mpg,main="box plot",ylab="miles per gallon")
dt<-mtcars
#绘制分组的箱线图
boxplot(mpg~cyl,data=mtcars,varwidth=TRUE,notch=TRUE)#notch便于比较中位线,VARWIDTH根据样本数量来调整宽度
#绘制二维的箱线图
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("pink","blue"))
#6.5.2小提琴图:箱线图和核密度图的叠加
install.packages("vioplot")
library(vioplot)
x1<-mtcars$mpg[mtcars$cyl==4]
x2<-mtcars$mpg[mtcars$cyl==6]
x3<-mtcars$mpg[mtcars$cyl==8]
class(x1)
vioplot(x1,x2,x3,names=c("4","6","8"),col="gold")
6、点图
options(repos=structure(c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")))
#6.6点图
#6.6.1原始图
dotchart(mtcars$mpg,labels = row.names(mtcars),cex = .7,
main="gas mileage for car models",
xlab = "miles per gallon")
#6.6.2排序后的点图
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)