R基本图形

来源:互联网 发布:小米手机怎么开数据 编辑:程序博客网 时间:2024/06/09 22:45
#条形图
x<-c("none","some","none","some","improved","improved","none","some")
counts<-table(x)
#barplot(counts,main="The first test",xlab="type",ylab="counts")
#barplot(counts,horiz = TRUE,main="The first test",ylab="type",xlab="counts")
y<-c("some","some","none","some","some","improved","none","some")
counts2<-table(x,y)
#barplot(counts2,main="Test2",xlab="x",ylab="y",legend=row.names(counts2))
barplot(counts2,main="Test2",xlab="x",ylab="y",legend=row.names(counts2),beside = TRUE)


#排序后均值的条形图
states<-data.frame(state.region,state.x77)
means<-aggregate(states$Illiteracy,by=list(state.region),FUN=mean)
means<-means[order(means$x),]
barplot(means$x,main="Test3",names.arg=means$Group.1)


#剌状图---容易比较各因素所占的比例
library(vcd)
spine(counts2,main="Test4")


#饼图
par(mfrow=c(2,2))
slices<-c(10,12,4,16,8)
lbls<-c("US","UK","Australia","Germany","France")
pie(slices,lbls,main="Simple")


pct<-round(slices/sum(slices)*100)
labls<-paste(lbls," ",pct,"%",sep="")
pie(slices,labels = labls,col=rainbow(length(labls)),main="Pie Chart")


library(plotrix)
pie3D(slices,labels = labls,col=rainbow(length(labls)),main="3D Pie Chart")


mytable<-table(state.region)
lab<-paste(names(mytable)," ",mytable,sep=" ")
pie(mytable,lab,main="Test5")


#扇形图----同时展示相对数量和相互差异
library(plotrix)
slices<-c(10,12,4,16,8)
lbl<-c("US","UK","UQ","UW","UR")
fan.plot(slices,labels=lbl,main="Test6")


#直方图---连续型变量
par(mfrow=c(2,2))
hist(mtcars$mpg)


hist(mtcars$mpg,breaks = 12,col="red",xlab="x",main="Test7")


hist(mtcars$mpg,freq=FALSE,breaks = 12,col="red",xlab="x",main="Test7",rug(jitter(mtcars$mpg)))
lines(density(mtcars$mpg),col="blue",lwd=2)


x<-mtcars$mpg
h<-hist(x,breaks = 12,col="red",xlab="x",main="Test7")
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(mfrow=c(2,1))
d<-density(mtcars$mpg)
plot(d)


d<-density(mtcars$mpg)
plot(d)
polygon(d,col="red",border="blue")
rug(mtcars$mpg,col="brown")


#可比较的核密度图
par(lwd=2)
library(sm)
attach(mtcars)
cyll<-factor(cyl,levels = c(4,6,8),labels = c("4","6","8"))
sm.density.compare(mpg,cyll,xlab="x",main="Test8")
colfill<-c(2:(1+length(levels(cyll))))
#交互式图例的位置
legend(locator(1),levels(cyll),fill=colfill)
detach(mtcars)


#箱线图
boxplot(mtcars$mpg,main="Test9",ylab="y")
#用于跨组比较
boxplot(mpg~cyl,data=mtcars,main="Test9",xlab="x",ylab="y")


#两个交叉因子的箱线图
x$cyl.f<-factor(mtcars$cyl,levels = c(4,6,8),labels = c("4","6","8"))
x$am.f<-factor(mtcars$am,levels = c(0,1),labels=c("auto","standard"))
boxplot(mpg~am.f*cyl.f,data=x,varwidth=TRUE)


#小提琴图
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","6","8"),col="red")


#点图--分组排序用不同符号和颜色区分时比较有用
dotchart(mtcars$mpg,labels=row.names(mtcars),cex=.7)


x<-mtcars[order(mtcars$mpg),]
x$col[x$cyl==4]<-"red"
x$col[x$cyl==6]<-"blue"
x$col[x$cyl==8]<-"yellow"
dotchart(x$mpg,groups = x$cyl,color = x$col,labels = row.names(x),cex=.7,pch=c(17,18,19),gcolor = "black")
0 0
原创粉丝点击