[R] Second y-axis --- alternative to par(new=
Petr PIKAL
petr.pikal at precheza.cz
Wed Oct 7 12:26:31 CEST 2009
Hi
Rainer M Krug <r.m.krug at gmail.com> napsal dne 07.10.2009 12:09:21:
>
> On Wed, Oct 7, 2009 at 11:53 AM, Petr PIKAL <petr.pikal at precheza.cz>
wrote:
> r-help-bounces at r-project.org napsal dne 07.10.2009 11:37:22:
>
> > On Wed, Oct 7, 2009 at 11:28 AM, Petr PIKAL <petr.pikal at precheza.cz>
> wrote:
> >
> > > Hi
> > >
> > > Try ?twoord.plot from plotrix package.
> > >
> > >
> > Thanks Petr - I was not aeware of this command.
> > But I would prefer a more general solution. An example on where I am
> using
> > is, to plot a histogram overlayed by its density estimate (easier to
> digest
> > for others then only a density estimate).
> That is quite different from your example.
>
> Well - in both cases, I want to overlay two graphs - in my example, it
is a
> line and a point plot, in the other a histogram and a density estimate.
>
> I believe overlay of density
> curve over histogram was discussed several times e.g.
> http://tolstoy.newcastle.edu.au/R/e4/help/08/06/14198.html
>
> Thanks - but if I want to have the histogram with counts (freq=TRUE),
then I
> have exactly the situation I was talking about - sorry for not being
clear:
>
> x <- rnorm(200)
> hist(x, col = "blue", freq = TRUE)
> lines(density(x), col = "red", lwd = 2)
>
> i.e.
> left y-axis (histogram): count
> right y-axis (density estimate): probability density
> > And a newY() (or par(newY=TRUE) command would enable exactly this.
> This is something beyond my knowledge and it could be that this need
some
> deep redefining in base R graphics.
>
> That's what I guess as well - let's see.
In the meantime, what about
x <- rnorm(200)
hist(x, col = "blue", freq = TRUE)
ddd<-density(x)
lines(ddd$x, ddd$y*100, col = "red", lwd = 2)
and then add suitable y axis.
axis(4, at=pretty(ddd$y*100), labels=pretty(ddd$y), col=2)
Regards
Petr
>
> Rainer
>
> Regards
> Petr
>
> >
> > Regards,
> >
> > Rainer
> >
> >
> >
> > > Regards
> > > Petr
> > >
> > >
> > >
> > > r-help-bounces at r-project.org napsal dne 07.10.2009 10:29:53:
> > >
> > > > Hi
> > > >
> > > > is there an alternative to par(new), for ading data to a plot for
a
> > > > different y-axis?
> > > > My problem with par(new=TRUE) is, that it re-defines all axis and
> labels
> > > (as
> > > > in example 1) and one has to use xlim=... to fix the x-axis.
> > > > I am looking for something, which simply resets the y-axis, so
that
> a
> > > new
> > > > plot() (or points()/lines()) keeps the x-axis, but re-defines the
> > > y-axis.
> > > >
> > > > Is there something available? I could re-scale the y-data to fit
the
> > > > existing y-axis, but that would mean to define all the labels and
> > > tickmarks
> > > > for axis 4 manually.
> > > >
> > > > HEre is an example about what I mean:
> > > >
> > > > x1 <- 1:10
> > > > y1 <- runif(10)
> > > > x2 <- 1:11
> > > > y2 <- c(y1*100, 0)
> > > >
> > > > ## (1) Does not plot points where (x-axis) they should be
> > > > plot(x1, y1, type="l")
> > > > par(new=TRUE)
> > > > plot(x2, y2, type="p")
> > > > axis(4)
> > > >
> > > > ## (2) Does plot points where (x-axis) they should be
> > > > xlim <- range(x1)
> > > > plot(x1, y1, type="l", xlim=xlim)
> > > > par(new=TRUE)
> > > > plot(x2, y2, type="p", xlim=xlim)
> > > > axis(4)
> > > >
> > > > Cheers,
> > > >
> > > >
> > > > Rainer
> > > > --
> > > > Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
> > > Biology,
> > > > UCT), Dipl. Phys. (Germany)
> > > >
> > > > Centre of Excellence for Invasion Biology
> > > > Natural Sciences Building
> > > > Office Suite 2039
> > > > Stellenbosch University
> > > > Main Campus, Merriman Avenue
> > > > Stellenbosch
> > > > South Africa
> > > >
> > > > Cell: +27 - (0)83 9479 042
> > > > Fax: +27 - (0)86 516 2782
> > > > Fax: +49 - (0)721 151 334 888
> > > > email: Rainer at krugs.de
> > > >
> > > > Skype: RMkrug
> > > > Google: R.M.Krug at gmail.com
> > > >
> > > > [[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.
> > >
> > >
> >
> >
> > --
> > Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
> Biology,
> > UCT), Dipl. Phys. (Germany)
> >
> > Centre of Excellence for Invasion Biology
> > Natural Sciences Building
> > Office Suite 2039
> > Stellenbosch University
> > Main Campus, Merriman Avenue
> > Stellenbosch
> > South Africa
> >
> > Cell: +27 - (0)83 9479 042
> > Fax: +27 - (0)86 516 2782
> > Fax: +49 - (0)721 151 334 888
> > email: Rainer at krugs.de
> >
> > Skype: RMkrug
> > Google: R.M.Krug at gmail.com
> >
> > [[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.
>
>
>
> --
> Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
Biology,
> UCT), Dipl. Phys. (Germany)
>
> Centre of Excellence for Invasion Biology
> Natural Sciences Building
> Office Suite 2039
> Stellenbosch University
> Main Campus, Merriman Avenue
> Stellenbosch
> South Africa
>
> Cell: +27 - (0)83 9479 042
> Fax: +27 - (0)86 516 2782
> Fax: +49 - (0)721 151 334 888
> email: Rainer at krugs.de
>
> Skype: RMkrug
> Google: R.M.Krug at gmail.com
More information about the R-help
mailing list