##基本图形
#条形图
counts=table(Arthritis$Improved)
counts
#简单条形图
barplot(counts,main="simple bar plot",xlab="improvement",ylab="Frequency")
#水平条形图
barplot(counts,main="horizontal bar plot",xlab="frequence",ylab="improvement",horiz=TRUE)
#若要绘制的类别型变量是一个因子或者有序型因子,就可以用plot()
plot(Arthritis$Improved,horiz=TRUE,main="simple bar plot",xlab="improved",ylab="frequence")
#堆砌条形图和分组条形图
counts=table(Arthritis$Improved,Arthritis$Treatment)
counts
barplot(counts,main="Stack 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)
#均值条形图
states=data.frame(state.region,state.x77)
means=aggregate(states$Illiteracy,by=list(state.region),FUN=mean)
means
means=means[order(means$x),]#将均值从小到大排序
means
barplot(means$x,names.arg = means$Group.1)
title("Mean Illiteracy Rate")#添加标题,与plot里的main等价
##棘状图
attach(Arthritis)
counts=table(Treatment,Improved)
spine(counts,main="Spinogram Example")
detach(Arthritis)
#饼图pie(x,labels)
par(mfrow=c(2,2))
slices=c(10,12,4,16,8)
lbls=c("US","UK","Australia","Germany","France")
#以上工作将四幅图组合为一幅图
pie(slices,labels=lbls,main="Simple Pie Chart")
pct=round(slices/sum(slices)*100)
lbls2=paste(lbls," ",pct,"%",seq=" ")
pie(slices,labels=lbls2,col=rainbow(length(lbls2)),main =
"pie chart with percentage")
pie3D(slices,labels=lbls,explode=0.1,
main="3D pie chart")
##从表格创建饼图
mytable=table(state.region)
lbls3=paste(names(mytable),"\n",mytable,seq="")
pie(mytable,labels = lbls3,
main="pie chart from a table\n (with sample sizes)")
#扇形图
slices=c(10,12,4,16,8)
lbls=c("US","UK","Australia","Germany","France")
fan.plot(slices,labels = lbls,main="Fan plot")
#直方图
#hist()
par(mfrowc(2,2))
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")
##核密度图
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")
#箱型图boxplot(),点图dotchart()
R语言基本图形
最新推荐文章于 2022-03-10 11:18:35 发布