R语言学习-基本图形-条形图

匿名 (未验证) 提交于 2019-12-03 00:30:01
#1.条形图
#1.1水平or竖直条形图 :向量绘图
library(vcd)
library(grid)

counts<- table(Arthritis$Improved)
str(counts)
> counts



barplot(counts)#简单条形图



barplot(counts,horiz = T)#水平条形图 horiz = T


a<-c(1,2,3,5)

barplot(a)#向量绘图


plot(Arthritis$Improved) #绘图效果同 barplot(counts)

#1.2堆砌条形图or分组条形图 : 矩阵绘图


cells<-c(1,2,3,4)
rnames<-c("R1","R2")
cnames<-c("C1","C2")
m<-matrix(cells,nrow=2,ncol=2,dimnames = list(rnames,cnames))
> m




barplot(m,col=c("blue","lightblue"),legend=rownames(m))#矩阵绘图


counts<- table(Arthritis$Improved,Arthritis$Treatment)
str(counts)
> counts







barplot(counts,col=c("red","blue","green"),legend=rownames(counts))#堆砌条形图


barplot(counts,col=c("red","blue","green"),legend=rownames(counts),beside = T)#分组条形图:beside = T

#1.3均值条形图
state<-data.frame(state.region,state.x77)
means<-aggregate(state$Illiteracy,by=list(state.region ),mean)

means<-means[order(means$x),]

> means

3 North Central 0.700000



barplot(means$x,names.arg = means$Group.1)#names.arg展示标签


lines(means$x)#将各个条形用线段连接起来


title("TITLE")#标题

#1.4棘状图:对堆砌条形图进行了重缩放,使得每个条形的高度均为1,每一段的比例即为比例
counts<- table(Arthritis$Improved,Arthritis$Treatment)
str(counts)
> counts

spine(counts)

#1.5条形图的微调
#cex.names减小字号,指定为小于1的值则缩小标签大小


par(mar=c(5,8,4,2))#增加Y边界的大小
par(las=1)#旋转条形的标签 las取值:1(普通情况),2(横坐标旋转),3 (横纵坐标均旋转)

counts<-table(Arthritis$Improved)

> counts



barplot(counts,cex.names = 0.4,#缩小字体的大小


las=1


las=2


las=3


las=1,cex.names = 0.4





易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!