[R] how to plot histogram plot and fitted distributions on the same graph

Albyn Jones jones at reed.edu
Thu Jan 8 18:41:15 CET 2009


You are plotting the histogram in the frequency scale.  A quick look  
at the doc page for hist() would reveal the freq option:

       hist(x,freq=FALSE)

then you can add the densities with lines()

albyn

Quoting Xin Shi <jasonshi510 at hotmail.com>:

> Dear:
>
>
>
> I am trying to plot the histogram graph for my observed data. Then plot
> fitted distribution on the same graph of histogram plot in R.
>
>
>
> 1.	histogram plot y.
> 2.	based on "1", plotting y1 v. x;
> 3.	based on "1", plotting y2 v. x;
> 4.	based on "1", plotting y3 v. x;
>
>
>
> All of these four plots must be on the same graph.
>
>
>
> However, I found the difficulty is that the y-axis and x-axis for histogram
> plot and fitted distribution plot are different.
>
>
>
> For histogram plot, y presents the frequency and x presents events.
>
>
>
> For fitted distribution plots, y presents the probability and x presents
> another variable.
>
>
>
> However, I found out I need histogram plot rather than barplot. This is
> major problem of this work.
>
>
>
> The code I used:
>
>
>
> par(font=1,font.lab=10,font.axis=6)
>
> pts18=barplot(y,
> ylim=c(0,0.2),xlim=c(2,52),axes=FALSE,border=TRUE,names.arg=x,col="white")
>
> axis(2,las=1)
>
> lines(spline(pts18,y1,n=300,method="natural"),type="l",lty=1)
>
> lines(spline(pts18,y2,n=300,method="natural"),type="l",lty=2)
>
> lines(spline(pts18,y3,n=300,method="natural"),type="l",lty=5)
>
>
>
> The data are:
>
>
>
> The observed data:
>
>
>
> y<-c(0.098441926, 0.166430595, 0.121813031, 0.104815864, 0.074362606,
>
> 0.075779037, 0.055949008, 0.040368272, 0.03470255, 0.029745042,
>
> 0.032577904, 0.02266289, 0.014872521, 0.014872521, 0.010623229,
>
> 0.01203966, 0.01203966, 0.008498584, 0.009206799, 0.009915014,
>
> 0.006373938, 0.003541076, 0.001416431, 0.001416431, 0.005665722,
>
> 0.002124646, 0.000708215, 0.001416431, 0.004249292, 0.002832861,
>
> 0.004957507, 0.002124646, 0.000708215, 0, 0.000708215, 0.002124646,
>
> 0.001416431, 0.001416431, 0.001416431, 0, 0.000708215)
>
>
>
> Fitted distribution 1:
>
>
>
> y1<-c(0.03419162, 0.154201321, 0.129581481, 0.108892454, 0.091506645,
>
> 0.07689666, 0.064619311, 0.054302168, 0.045632264, 0.0383466,
>
> 0.032224168, 0.027079245, 0.022755763, 0.01912257, 0.016069453,
>
> 0.013503798, 0.011347777, 0.009535987, 0.008013468, 0.006734034,
>
> 0.005658876, 0.004755378, 0.003996132, 0.003358108, 0.002821952,
>
> 0.002371398, 0.00199278, 0.001674612, 0.001407243, 0.001182562,
>
> 0.000993753, 0.00083509, 0.00070176, 0.000589716, 0.000495562,
>
> 0.00041644, 0.000349951, 0.000294078, 0.000247125, 0.000207669,
>
> 0.000174513)
>
>
>
> Fitted distribution 2:
>
>
>
> y2<-c(0.078909441, 0.188048499, 0.117871979, 0.089827482, 0.072368317,
>
> 0.059928019, 0.050453301, 0.042948906, 0.036851702, 0.031809247,
>
> 0.027584779, 0.024010745, 0.020963795, 0.01835029, 0.016097393,
>
> 0.014147335, 0.012453559, 0.010978051, 0.009689433, 0.008561564,
>
> 0.007572497, 0.006703683, 0.005939358, 0.005266055, 0.004672222,
>
> 0.004147912, 0.003684531, 0.003274633, 0.002911751, 0.00259025,
>
> 0.002305216, 0.002052353, 0.001827898, 0.001628552, 0.001451415,
>
> 0.001293939, 0.001153881, 0.001029262, 0.000918338, 0.000819567,
>
> 0.000731589)
>
>
>
> Fitted distribution 3:
>
>
>
> y3<-c(0.09844545, 0.174856171, 0.1190666, 0.093021492, 0.075639902,
>
> 0.062740817, 0.052668044, 0.044568247, 0.037931599, 0.032423244,
>
> 0.027808545, 0.023915327, 0.020612892, 0.01779946, 0.015394205,
>
> 0.013331948, 0.011559483, 0.010032949, 0.008715898, 0.007577845,
>
> 0.006593146, 0.005740133, 0.005000424, 0.004358371, 0.003800615,
>
> 0.003315725, 0.002893892, 0.002526689, 0.002206859, 0.001928146,
>
> 0.001685148, 0.001473194, 0.001288243, 0.001126794, 0.00098581,
>
> 0.000862657, 0.000755047, 0.000660991, 0.000578759, 0.000506847,
>
> 0.000443945)
>
>
>
> x<- c(0, 1, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55,
>
> 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115,
>
> 120, 125, 130, 135, 140, 145, 150, 155, 160, 165, 170,
>
> 175, 180, 185, 190, 200)
>
>
>
> Many Thanks!
>
>
>
> Xin
>
>
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>




More information about the R-help mailing list