[R] how to plots two pairwise data sets into a same graph

David Winsemius dwinsemius at comcast.net
Mon May 5 16:02:08 CEST 2008


"Xin" <jasonshi510 at hotmail.com> wrote in
news:BAY141-DAV13BBD81C4AB9BEFD23898AF0D70 at phx.gbl: 

> Dear all: 
> 
>    I fitted "Observed" into a distribution as frequency. The
>    predicted values are calculated as "predicted" for frequency.
>    bins is "x". I plot observed, predicted against x in a graph. the
>    commond is here. 
> 
> pts=barplot(observed,xlab="points",ylab="Frequency",ylim=c(0,300),xli
> m=c(2,52),axes=FALSE,border=TRUE,names.arg=x,col="white") 
> lines(spline(pts,predicted,n=300,method="natural"),type="l",lty=5) 
> 
> However, I also want to plot the corresponding distribution. Then I
> calculated "fits" for "x-fits". But I want to plot x-fits against
> fits on the same graph with the obove one. The problem is the number
> of data points are different for these two data set. This is because
> one is barplot for frequency and the other is distribution. 
> 
> Anyone has expreience on this? 

snipped "observed" and "predicted" assignment.
> 
> x-fits=c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,

I am puzzled by the name of your third R object. My version of R will 
not allow me to assign a value to "x-fits". It parses the dash as a 
minus-sign. The usual approach is to use a period or underscore ("_") 
in variable names.

If you try the example in barplot's help page and then experiment with 
it a bit, you may see a method that can be generalized to solve your 
problems:

The example:
> require(grDevices) # for colours
> tN <- table(Ni <- stats::rpois(100, lambda=5))
> r <- barplot(tN, col=rainbow(20))
> #- type = "h" plotting *is* 'bar'plot
> lines(r, tN, type='h', col='red', lwd=2)

# Change the line type to prove that you can get a "connected" line:
lines(r, tN, col='red', lwd=2)

# Change the "x" argument to lines  (or points() )to prove that you 
# are not constrained to replicate the "x" used by barplot.

lines(r +.5, tN, col='red', lwd=4)
points(1,6)
points(1.7,6)

-- 
David Winsemius



More information about the R-help mailing list