1 setwd("C:\\RBook\\数据研究\\重庆数据研究\\面积\\") 2 3 mhist<-function(sData,freq="T",Color="black",lty="solid",brk=NULL) 4 { 5 exf<-hist(sData,plot="F",breaks=brk) 6 brk<-exf$breaks 7 if(freq) 8 cnt<-exf$intensities 9 else 10 cnt<-exf$counts 11 12 for(i in 1:length(cnt)) 13 { 14 n=i+1 15 #第一条竖线 16 lines(c(brk[i],brk[i]),c(0,cnt[i]),lty=lty,col=Color) 17 #第二条竖线 18 lines(c(brk[n],brk[n]),c(0,cnt[i]),lty=lty,col=Color) 19 #顶线 20 lines(c(brk[i],brk[n]),c(cnt[i],cnt[i]),lty=lty,col=Color) 21 } 22 } 23 ##三个数据文件。只有一列 mp 24 files=c("all.csv","lessT90.csv","90T120.csv","greaterT120.csv") 25 26 xMin=NULL 27 xMax=NULL 28 yMin=NULL 29 yMax=NULL 30 mp_seq=300 31 brk=seq(0,15000,mp_seq) 32 33 for(fil in files) 34 { 35 d<-read.table(fil,header=T) 36 exf<-hist(d$mp,plot="F",brk) 37 xMin=min(xMin,exf$breaks) 38 xMax=max(xMax,exf$breaks) 39 yMin=min(yMin,exf$intensities) 40 yMax=max(yMax,exf$intensities) 41 } 42 plot(x=c(xMin,xMax),xlab="",ylab="",xaxt="n",frame = FALSE,main="2012年上半年不同面积段的均价直方",y=c(yMin,yMax),type="n") 43 44 axis(1,brk,pos=0,las=2) 45 ltys<-c("solid","dashed","dotdash","dotted") 46 cols<-c("black","green","red","blue") 47 legends<-c("all","<90","90-120",">=120") 48 49 for(i in 1:length(files)) 50 { 51 d<-read.table(files[i],header=T) 52 mhist(d$mp,freq="T",Color=cols[i],lty=ltys[i],brk=brk) 53 } 54 legend(x=12000,y=2e-04,lty=ltys,col=cols,legend=legends)
结果如下: