[R] distribution graph

Marc Schwartz marc_schwartz at comcast.net
Fri Mar 23 17:55:40 CET 2007


On Fri, 2007-03-23 at 14:22 +0000, ted.harding at nessie.mcc.ac.uk wrote:
> [Apologies -- there were errors in the code I posted previously.
>  A corrected version is below]
> 
> On 23-Mar-07 11:06:49, Plessen, Christian von wrote:
> > 
> > I am looking for a way to produce a "distribution graph" as in the
> > example: 
> > 
> > (http://cecsweb.dartmouth.edu/release1.1/datatools/dgraph.php?year=2003&
> > geotype=STD_HRR&event=A01_DIS&eventtype=UTIL
> > 
> > Anybody who can help?
> > 
> 
> The following (which anyway needs refinement, and can very
> probably be done better) provides a basis (illustrated using
> a sample from a log-normal distribution):
> 
> 
> X<-exp(rnorm(200,sd=0.25)+2)/5
> 
> H<-hist(X,breaks=20)
> C<-H$counts
> Y<-H$mids
> C1<-C/2
> 
> C0<-(-(C1[1]-1/2)):(C1[1]-1/2); n0<-length(C0)
> plot(C0,rep(Y[1],n0),xlim=c(-max(C)/2,max(C)/2),ylim=c(min(Y),max(Y)))
> 
> for(i in (2:length(Y))){
>   if(C[i]==0) next
>   C0 <- (-(C1[i] - 1/2)):(C1[i] - 1/2); n0<-length(C0)
>   points(C0,rep(Y[i],n0))
> }
> 
> 
> Hoping this helps!
> Ted.


How about something like this:


DistPlot <- function(x, digits = 1, ...)
{
  x <- round(x, digits)
  
  Tab <- table(x)

  Vals <- sapply(Tab, function(x) seq(x) - mean(seq(x)))

  X.Vals <- unlist(Vals, use.names = FALSE)
  tmp <- sapply(Vals, length)
  Y.Vals <- rep(names(tmp), tmp)

  plot(X.Vals, Y.Vals, ...)
}


Vec <- exp(rnorm(200, sd = 0.25) + 2) / 5

DistPlot(Vec, pch = 19)



HTH,

Marc Schwartz



More information about the R-help mailing list