绘制基础条形图
barplot(height, width = 1, space = NULL, names.arg = NULL, legend.text = NULL, beside = FALSE, horiz = FALSE, density = NULL, angle = 45, col = NULL, border = par("fg"), main = NULL, sub = NULL, xlab = NULL, ylab = NULL, xlim = NULL, ylim = NULL, xpd = TRUE, log = "", axes = TRUE, axisnames = TRUE, cex.axis = par("cex.axis"), cex.names = par("cex.axis"), inside = TRUE, plot = TRUE, axis.lty = 0, offset = 0, add = FALSE, args.legend = NULL, ...)
基础配置
main,sub:图的整体和分标题。xlab和ylab:设置x轴与y轴的lable。xlim和ylim:设置图形x轴与y轴的范围。axes:逻辑参数。设置图形是否显示x轴或y轴。axis.lty:设置x轴的类型(类似lty)。默认axis.lty=0.axis(1,labels=month,at=1:31,las=3) 1表示要对x轴作修改,labels即显示的内容,at就是显示多少个刻度(这里只能是1到31),las 刻度的显示形式,3就是竖着axisnames:逻辑参数。设置是否显示条形标签。names.arg:设置条形标签(bar labels)。cex.axis:设置坐标轴数值的膨胀率。比如cex.axis=1.5cex.names:设置条形标签(bar labels)的膨胀率。比如cex.axis=1.5.width:设置条形的宽度。space:设置各个条形间的宽度。相当于各个条形宽度的一部分。默认c(0,1)beside:逻辑参数。如果FALSE,那么将绘画堆叠式的条形;如果是TRUE,将绘画并列式条形。horiz:逻辑参数。设置图形是水平或是垂直。density:底纹的密度。默认值为NULL。angle:设置底纹的斜率col:设置条形底纹或者填充颜色。border:设置条形边缘颜色。如果设置为NA,则消除了边缘。xpd: 条形应该被允许以外的区域
data =c(runif(30,0,9999)) barplot(data,main="条形统计图",sub="子标题",xlab="X轴",ylab="Y轴")
横向
绘制复杂多种图形
加载颜色面板
library(RColorBrewer) ## 加载颜色面板 color=brewer.pal(7,"Set1")#存储颜色
生成随机数据
数学 = round(runif(7,70,100),3) 语文 = round(runif(7,50,100),3) 英语 = round(runif(7,50,100),3) 生物 = round(runif(7,50,100),3) 物理 = round(runif(7,50,100),3) 地理 = round(runif(7,50,100),3) 历史 = round(runif(7,50,100),3)
创建数据
tables = data.frame(数学,语文,英语,生物,物理,地理,历史) matrix = t(as.matrix(tables))
#每次模拟总成绩 sum = apply(matrix, 2, sum) #每次模拟数学所占比例 ratioSX= matrix[1,]/sum
设置标签
lables = c("模拟1","模拟2","模拟3","模拟4","模拟5","模拟6","模拟7")
设置绘图区域边界
par(mar=c(5,4,6,5)) mybar = barplot(matrix,col=color[1:7],space = 0.5,axes = F,names.arg = lables,cex.names = .8,xlim = c(0,11),ylim = c(0,800)) #绘制条形图
确保绘图区域外部能添加元素
par(xpd=T) legend(1,900,legend = colnames(tables),fill = color[1:7],border = F,ncol = 7,cex=.8,x.intersp = .2,y.intersp = .5,text.font = 1,bty = "n",text.width = .3)#添加图例
添加左坐标轴
axis(side = 2,at = seq(0,800,400),cex.axis=.8,line = 0)
设置绘图边界
par(new=T) #添加新图层 par(mar=c(3.5,4,6,4)) #设置绘图边界
添加点线图
plot(mybar,ratioSX,type="b",axes=F,ann=F,xlim = c(0,11),ylim = c(0,0.18),col='black',lwd=2,pch=19)#添加点线图
添加右坐标轴
axis(side = 4,at = seq(0,0.18,0.02),cex.axis=.8,line = -3)#添加右坐标轴
添加图例
legend(0.1,0.15,legend = "数学比例",col="black",bty="n",pch=19,x.intersp = .3,lty=1,lwd=2)#添加图例
添加坐标轴标题
mtext("分数",side = 2,font = 2,cex=1)#添加坐标轴标题 mtext("百分比",side = 4,font = 2,cex=1)
绘制图形
par(mypar)#恢复默认绘图参数
数据问题导致折线图还有点问题。
全部代码
library(RColorBrewer) ## 加载颜色面板 color=brewer.pal(7,"Set1")#存储颜色
数学 = round(runif(7,70,100),3) 语文 = round(runif(7,50,100),3) 英语 = round(runif(7,50,100),3) 生物 = round(runif(7,50,100),3) 物理 = round(runif(7,50,100),3) 地理 = round(runif(7,50,100),3) 历史 = round(runif(7,50,100),3)
tables = data.frame(数学,语文,英语,生物,物理,地理,历史) matrix = t(as.matrix(tables))
#每次模拟总成绩 sum = apply(matrix, 2, sum) #每次模拟数学所占比例 ratioSX= matrix[1,]/sum
lables = c("模拟1","模拟2","模拟3","模拟4","模拟5","模拟6","模拟7")
#设置绘图区域边界 par(mar=c(5,4,6,5)) mybar = barplot(matrix,col=color[1:7],space = 0.5,axes = F,names.arg = lables,cex.names = .8,xlim = c(0,11),ylim = c(0,800)) #绘制条形图 #确保绘图区域外部能添加元素 par(xpd=T) legend(1,900,legend = colnames(tables),fill = color[1:7],border = F,ncol = 7,cex=.8,x.intersp = .2,y.intersp = .5,text.font = 1,bty = "n",text.width = .3)#添加图例
axis(side = 2,at = seq(0,800,400),cex.axis=.8,line = 0)#添加左坐标轴
par(new=T) #添加新图层 par(mar=c(3.5,4,6,4)) #设置绘图边界 plot(mybar,ratioSX,type="b",axes=F,ann=F,xlim = c(0,11),ylim = c(0,0.18),col='black',lwd=2,pch=19)#添加点线图 axis(side = 4,at = seq(0,0.18,0.02),cex.axis=.8,line = -3)#添加右坐标轴 legend(0.1,0.15,legend = "数学比例",col="black",bty="n",pch=19,x.intersp = .3,lty=1,lwd=2)#添加图例 mtext("分数",side = 2,font = 2,cex=1)#添加坐标轴标题 mtext("百分比",side = 4,font = 2,cex=1)
par(mypar)#恢复默认绘图参数