用R画直方图

平时在做数据分析的时候很多图都是用excel画,包括直方图。今天给大家分享两段用R画直方图的代码。

代码一:

[code lang="R"]
data=c(4.51,10.69,9.33,7.34,5.09,11.68,4.47,8.53,13.99,5.22,4.22,9.23,7.86)
labs=c("Species1","Species2","Species3", "Species4", "Species5", "Species6", "Species7", "Species8", "Species9", "Species10", "Species11", "Species12", "Species13")
barplot(data,col=c("steelblue","steelblue","steelblue","mediumturquoise","mediumturquoise","mediumturquoise","mediumturquoise", "mediumturquoise","mediumturquoise","sandybrown","hotpink","hotpink","hotpink"),ylim=c(0,14),width=1,space=1,ylab="%(......)",las=1)
text(x=seq(1.5,25.5,by=2),y=-0.15, srt = 45, adj = 1, labels = labs,xpd = TRUE)
abline(h=c(2,4,6,8,10,12,14),col="#00000088",lwd=2)
abline(h=0)
[/code]

效果图:

用R画直方图-图片1

代码二:

[code lang="R"]
labs=c("Species1","Species2","Species3", "Species4", "Species5", "Species6", "Species7", "Species8", "Species9", "Species10", "Species11", "Species12", "Species13")
mydata<-cbind(c(2017,400,5013,308),c(640,2998,1798,4530),c(560,300,750,922),c(4654,323,3432,710),c(249,3246,2490,3604),c(746,200,990,3871),c(150,2419,1700,937),c(9801,741,144,1118),c(1651,5778,8056,1040),c(196,345,456,2108),c(246,413,214,1605),c(495,107,1582,820),c(885,501,1618,1881))
barplot(mydata,col=c("royalblue","firebrick","yellowgreen","darkorchid","darkorchid"),width=1,space=1,border=NA,legend.text=c("Name1","Name2","Name3","Name4"),args.legend=list(x="topright"))
abline(h=0)
text(x=seq(1.5,25.5,by=2),y=-300, srt = 45, adj = 1, labels = labs,xpd = TRUE)
[/code]

效果图:

用R画直方图-图片2

发表评论

匿名网友

拖动滑块以完成验证