R users: I want X-Y plotting with axes in reverse direction such as (0, -1, -2, -3, ....). How can I do it? Thanks in advance Xiao
On Sun, 2004-06-27 at 18:24, XIAO LIU wrote:> R users: > > I want X-Y plotting with axes in reverse direction such as (0, -1, -2, > -3, ....). How can I do it? > > Thanks in advance > > XiaoIf I am understanding what you want, the following should give you an example: # Create x and y with negative values x <- -1:-10 y <- -1:-10 # Show regular plot plot(x, y) # Now plot using -x and -y # Do not plot the axes or annotation plot(-x, -y, axes = FALSE, ann = FALSE) # Now label both x and y axes with negative # labels. Use pretty() to get standard tick mark locations # and use rev() to create tick mark labels in reverse order axis(1, at = pretty(-x), labels = rev(pretty(x))) axis(2, at = pretty(-y), labels = rev(pretty(y))) HTH, Marc Schwartz
XIAO LIU wrote:> R users: > > I want X-Y plotting with axes in reverse direction such as (0, -1, -2, -3, ....). How can I do it? > > Thanks in advanceUse the xlim and ylim arguments to plot. x = -(1:10) y = rnorm(10) # Standard plot plot(x, y) # Reversed x-axis plot(x, y, xlim=rev(range(x))) -- Ross Ihaka Email: ihaka at stat.auckland.ac.nz Department of Statistics Phone: (64-9) 373-7599 x 85054 University of Auckland Fax: (64-9) 373-7018 Private Bag 92019, Auckland New Zealand
Assume I want to put this into a function, how do I redefine the margins to fit better. I did par(mar=c(4.1,4.1,5.1,2.1) inside the function, but then it is not possible anymore to change them, I just want to change the default for this plotting function, how ? #---------------------------------------------------------- #Function to be improved #----------------------------------------------------- plot.revy <- function(x,y,...) { plot(x,y,ylim=rev(range(y)),axes = FALSE,...) axis(2) axis(3) box() } Ross Ihaka wrote:> XIAO LIU wrote: > >> R users: >> >> I want X-Y plotting with axes in reverse direction such as (0, -1, -2, >> -3, ....). How can I do it? >> >> Thanks in advance > > > Use the xlim and ylim arguments to plot. > > x = -(1:10) > y = rnorm(10) > > # Standard plot > plot(x, y) > > # Reversed x-axis > plot(x, y, xlim=rev(range(x))) >
Henrik Andersson wrote:> Assume I want to put this into a function, how do I redefine the margins > to fit better. > > I did par(mar=c(4.1,4.1,5.1,2.1) inside the function, but then it is not > possible anymore to change them, I just want to change the default for > this plotting function, how ?You can save the old par values and restore them after creating your plot. # reset par values, saving the old ones oldpar = par(mar=c(4.1,4.1,5.1,2.1)) # create stunning customized plot here # restore the old par values par(oldpar) When embedding this in a function like yours it is better to use the on.exit() function to restore the old values. f = function(...) { oldpar = par(mar=c(4.1,4.1,5.1,2.1)) on.exit(par(oldpar)) # create stunning customized plot here } Error exits from the function will then also restore the old par values. -- Ross Ihaka Email: ihaka at stat.auckland.ac.nz Department of Statistics Phone: (64-9) 373-7599 x 85054 University of Auckland Fax: (64-9) 373-7018 Private Bag 92019, Auckland New Zealand