Dears, a way to define x and y positions in plots in relative numbers (e.g in fractions between 0 and 1 referring to relative positions inside the plot region) would really help me. One example I would need this to would be to add text via text() to a plot always at a defined spot, e.g the upper left corner. Until now I always determined maximum x and y values and used those, but defining relative positions straight away would be much easier. Possible solutions: 1. Predefined function Is there anything available that lets me run (for example): text(0.5,0.5,'middle') which always puts text on these relative points? 2. Create my own function It would be straightforward to create my own function that translates the relative number to the axes values in the actual plot, so that text(my.function(0.5,0.5),'middle') would do what I want. For this I would need to be able to somehow retrieve the axis limits for x and y axes. Is there any way I could do this after having called plot()? Thanks for your help! Jannis
scaling with relative units in plots or retrieving axes limits in plots
5 messages · Jannis, Ivan Calandra, Greg Snow
Hi, For 2., I don't know if it's possible to retrieve the axis limits, but you can surely specify them in your call to plot (with arguments xlim and ylim). That's a cheap solution and others probably have better ones. Ivan Le 5/18/2010 16:23, Jannis a ?crit :
Dears, a way to define x and y positions in plots in relative numbers (e.g in fractions between 0 and 1 referring to relative positions inside the plot region) would really help me. One example I would need this to would be to add text via text() to a plot always at a defined spot, e.g the upper left corner. Until now I always determined maximum x and y values and used those, but defining relative positions straight away would be much easier. Possible solutions: 1. Predefined function Is there anything available that lets me run (for example): text(0.5,0.5,'middle') which always puts text on these relative points? 2. Create my own function It would be straightforward to create my own function that translates the relative number to the axes values in the actual plot, so that text(my.function(0.5,0.5),'middle') would do what I want. For this I would need to be able to somehow retrieve the axis limits for x and y axes. Is there any way I could do this after having called plot()? Thanks for your help! Jannis
______________________________________________ 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.
Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. S?ugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra at uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php
Thanks for the replies! If anybody encounters a similar problem, the function that now does what I wanted is attached below.
Best
Jannis
trnsf.coords = function(array_x,array_y)
# This function transfers relative coordinates between 0 and 1 for two arrays with x
# and y values into the coordinate system of the current plot.
{
plot_extremes=par()$usr
x_min=plot_extremes[1]
x_max=plot_extremes[2]
y_min=plot_extremes[3]
y_max=plot_extremes[4]
x_trans=x_min+(array_x*x_max-x_min)
y_trans=y_min+(array_y*y_max-y_min)
output=list(x=x_trans,y=y_trans)
return(output)
}
--- Jannis <bt_jannis at yahoo.de> schrieb am Di, 18.5.2010:
Von: Jannis <bt_jannis at yahoo.de> Betreff: [R] scaling with relative units in plots or retrieving axes limits in plots An: r-help at stat.math.ethz.ch Datum: Dienstag, 18. Mai, 2010 14:23 Uhr Dears, a way to define x and y positions in plots in relative numbers (e.g in fractions? between 0 and 1 referring to relative positions inside the plot region) would really help me. One example I would need this to would be to add text via text() to a plot always at a defined spot, e.g the upper left corner. Until now I always determined maximum x and y values and used those, but defining relative positions straight away would be much easier. Possible solutions: 1. Predefined function Is there anything available that lets me run (for example): text(0.5,0.5,'middle') which always puts text on these relative points? 2. Create my own function It would be straightforward to create my own function that translates the relative number to the axes values in the actual plot, so that text(my.function(0.5,0.5),'middle') would do what I want. For this I would need to be able to somehow retrieve the axis limits for x and y axes. Is there any way I could do this after having called plot()? Thanks for your help! Jannis
______________________________________________ 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.
Look at the grconvertX and grconvertY functions for a built in solution with much more flexibility.
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Jannis
> Sent: Tuesday, May 18, 2010 8:50 AM
> To: r-help at stat.math.ethz.ch
> Subject: Re: [R] scaling with relative units in plots or retrieving
> axes limits in plots
>
> Thanks for the replies! If anybody encounters a similar problem, the
> function that now does what I wanted is attached below.
>
>
> Best
> Jannis
>
>
>
> trnsf.coords = function(array_x,array_y)
>
> # This function transfers relative coordinates between 0 and 1 for two
> arrays with x
> # and y values into the coordinate system of the current plot.
>
> {
> plot_extremes=par()$usr
> x_min=plot_extremes[1]
> x_max=plot_extremes[2]
> y_min=plot_extremes[3]
> y_max=plot_extremes[4]
>
> x_trans=x_min+(array_x*x_max-x_min)
> y_trans=y_min+(array_y*y_max-y_min)
> output=list(x=x_trans,y=y_trans)
> return(output)
>
> }
>
> --- Jannis <bt_jannis at yahoo.de> schrieb am Di, 18.5.2010:
>
> > Von: Jannis <bt_jannis at yahoo.de>
> > Betreff: [R] scaling with relative units in plots or retrieving axes
> limits in plots
> > An: r-help at stat.math.ethz.ch
> > Datum: Dienstag, 18. Mai, 2010 14:23 Uhr
> > Dears,
> >
> >
> > a way to define x and y positions in plots in relative
> > numbers (e.g in fractions? between 0 and 1 referring to
> > relative positions inside the plot region) would really help
> > me. One example I would need this to would be to add text
> > via text() to a plot always at a defined spot, e.g the upper
> > left corner. Until now I always determined maximum x and y
> > values and used those, but defining relative positions
> > straight away would be much easier. Possible solutions:
> >
> > 1. Predefined function
> > Is there anything available that lets me run (for
> > example):
> >
> > text(0.5,0.5,'middle')
> >
> > which always puts text on these relative points?
> >
> >
> >
> > 2. Create my own function
> > It would be straightforward to create my own function that
> > translates the relative number to the axes values in the
> > actual plot, so that
> >
> > text(my.function(0.5,0.5),'middle')
> >
> > would do what I want. For this I would need to be able to
> > somehow retrieve the axis limits for x and y axes. Is there
> > any way I could do this after having called plot()?
> >
> >
> >
> > Thanks for your help!
> >
> >
> > Jannis
> >
> >
> >
> >
> > ______________________________________________
> > 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.
> >
>
>
>
> ______________________________________________
> 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.
Sorry for the spamming, but there was a slight mistake in my function. Even though something similar most probably exists, a correct version is attached below, just in case :-)
trnsf.coords = function(x=c(),y=c())
# This function transfers relative coordinates between 0 and 1 for two arrays with x
# and y values into the coordinate system of the current plot.
{
plot_extremes=par()$usr
x_min=plot_extremes[1]
x_max=plot_extremes[2]
y_min=plot_extremes[3]
y_max=plot_extremes[4]
x_trans=x_min+(x*(x_max-x_min))
y_trans=y_min+(y*(y_max-y_min))
if (length(y)==0)
{
output=list(x=x_trans)
} else if (length(x) ==0) {
output=list(y=y_trans)
} else {
output=list(x=x_trans,y=y_trans)
}
return(output)
}
--- Jannis <bt_jannis at yahoo.de> schrieb am Di, 18.5.2010:
Von: Jannis <bt_jannis at yahoo.de>
Betreff: Re: [R] scaling with relative units in plots or retrieving axes limits in plots
An: r-help at stat.math.ethz.ch
Datum: Dienstag, 18. Mai, 2010 14:50 Uhr
Thanks for the replies! If
anybody? encounters a similar problem, the function
that now does what I wanted is attached below.
Best
Jannis
trnsf.coords = function(array_x,array_y)
# This function transfers relative coordinates between 0
and 1 for two arrays with x
# and y values into the coordinate system of the current
plot.
? ?
{
? ? plot_extremes=par()$usr
? ? x_min=plot_extremes[1]
? ? x_max=plot_extremes[2]
? ? y_min=plot_extremes[3]
? ? y_max=plot_extremes[4]
? ? x_trans=x_min+(array_x*x_max-x_min)
? ? y_trans=y_min+(array_y*y_max-y_min)
? ? output=list(x=x_trans,y=y_trans)
? ? return(output)
}
--- Jannis <bt_jannis at yahoo.de>
schrieb am Di, 18.5.2010:
Von: Jannis <bt_jannis at yahoo.de> Betreff: [R] scaling with relative units in plots or
retrieving axes limits in plots
An: r-help at stat.math.ethz.ch Datum: Dienstag, 18. Mai, 2010 14:23 Uhr Dears, a way to define x and y positions in plots in
relative
numbers (e.g in fractions? between 0 and 1 referring
to
relative positions inside the plot region) would
really help
me. One example I would need this to would be to add
text
via text() to a plot always at a defined spot, e.g the
upper
left corner. Until now I always determined maximum x
and y
values and used those, but defining relative
positions
straight away would be much easier. Possible
solutions:
1. Predefined function Is there anything available that lets me run (for example): text(0.5,0.5,'middle') which always puts text on these relative points? 2. Create my own function It would be straightforward to create my own function
that
translates the relative number to the axes values in
the
actual plot, so that text(my.function(0.5,0.5),'middle') would do what I want. For this I would need to be able
to
somehow retrieve the axis limits for x and y axes. Is
there
any way I could do this after having called plot()? Thanks for your help! Jannis
______________________________________________ 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.
______________________________________________ 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.