上文为大家介绍的是R语言中的线图与时间序列谱图,相信大家已经对R语言有一定的了解了,接下来小编就给大家说说关于柱形图,点图,饼图,直方图。
一、柱形图
install.packages("RColorBrewer") #if not already installed
library(RColorBrewer)
citysales<-read.csv("citysales.csv")
barplot(as.matrix(citysales[,2:4]), beside=TRUE,legend.text=citysales$City,
args.legend=list(bty="n",horiz=TRUE),
col=brewer.pal(5,"Set1"),
border="white",ylim=c(0,100),
ylab="Sales Revenue (1,000's of USD)",
main="Sales Figures")
box(bty="l")
二、用堆叠效果展示百分比
citysalesperc<-read.csv("citysalesperc.csv")
par(mar=c(5,4,4,8),xpd=T)
barplot(as.matrix(citysalesperc[,2:4]),
col=brewer.pal(5,"Set1"),border="white",
ylab="Sales Revenue (1,000's of USD)",
main="Percentage Sales Figures")
legend("right",legend=citysalesperc$City,bty="n",
inset=c(-0.3,0),fill=brewer.pal(5,"Set1"))
三、调整柱形图的宽度,间隔和颜色
barplot(as.matrix(citysales[,2:4]),beside=TRUE,
legend.text=citysales$City,args.legend=list(bty="n",horiz=T),
col=c("#E5562A","#491A5B","#8C6CA8","#BD1B8A","#7CB6E4"),
border=FALSE,space=c(0,5),ylim=c(0,100),ylab="Sales Revenue(1,000's of USD)",
main="Sales Figures")
四、在柱子顶端显示数据
x<-barplot(as.matrix(citysales[,2:4]),beside=TRUE,legend.text=citysales$City,
args.legend=list(bty="n",horiz=TRUE),col=brewer.pal(5,"Set1"),border="white",
ylim=c(0,100),ylab="Sales Revenue (1,000's of USD)",main="Sales Figures")
y<-as.matrix(citysales[,2:4])
text(x,y+2,labels=as.character(y))
五、标注误差
sales<-t(as.matrix(citysales[,-1]))
colnames(sales)<-citysales[,1]
x<-barplot(sales,beside=T,legend.text=rownames(sales),
args.legend=list(bty="n",horiz=T),
col=brewer.pal(3,"Set2"),border="white",ylim=c(0,100),
ylab="Sales Revenue (1,000's of USD)",main="Sales Figures")
arrows(x0=x,y0=sales*0.95,x1=x,y1=sales*1.05,angle=90,code=3,length=0.04,lwd=0.4)
六、点图
install.packages("reshape")
library(reshape)
sales<-melt(citysales)
sales$color[sales[,2]=="ProductA"] <- "red"
sales$color[sales[,2]=="ProductB"] <- "blue"
sales$color[sales[,2]=="ProductC"] <- "violet"
dotchart(sales[,3],labels=sales$City,groups=sales[,2],col=sales$color,pch=19,
main="Sales Figures",xlab="Sales Revenue (1,000's of USD)")
七、饼图
browsers<-read.table("browsers.txt",header=TRUE)
browsers<-browsers[order(browsers[,2]),]
pie(browsers[,2],labels=browsers[,1],clockwise=TRUE,radius=1,col=brewer.pal(7,"Set1"),
border="white",main="Percentage Share of Internet Browser usage")
八、一组直方图
panel.hist <- function(x, ...)
{
par(usr = c(par("usr")[1:2], 0, 1.5) )
hist(x,prob=TRUE,add=TRUE,col="black",border="white")
}
plot(iris[,1:4],
main="Relationships between characteristics of iris flowers",
pch=19,col="blue",cex=0.9,diag.panel=panel.hist)
关于R语言数据展现——柱形图,点图,饼图,直方图的讲述就到这里了,上述的内容只是截取了资料中的一部分,如需获取完整的资料,可通过关注微信公众号后联系客服获取。
想要了解更多的资料、信息,可持续关注我们,我们将为大家提供有价值、有需求的材料。为大家在整理数据时省去烦恼。