2

In general when we do plotting, the plot has the x-axis on bottom (left to right) and y-axis on left (bottom to top).
For example, In R-programming I have a code like this:

t <- seq(0,1,0.2)       # need t values in top x axis
plot(t,t^2,type="l")    # need t^2 values in inverted y-axis

Now, if we want plot so that the x-axis is on top (left to right) and y-axis inverted (top to bottom).
How can we achieve such a feat in R-programming? I searched following links in stackoverflow but they could not meet my requirements:
How to invert the y-axis on a plot

Invert y-axis in barplot

Community
  • 1
  • 1
BhishanPoudel
  • 15,974
  • 21
  • 108
  • 169

1 Answers1

8

Check out ?axis

t <- seq(0,1,0.2)
plot(t,t,type="l", xaxt = 'n', yaxt = 'n')
lines(t,t^2,col="green")
lines(t,t^3,col="blue")
axis(3)
axis(2, at = pretty(t), labels = rev(pretty(t)))

enter image description here

I'm not sure why the .0 gets dropped on the y, but you can use labels = format(rev(pretty(t)), digits = 1) for consistency

EDIT

to reverse the entire plot about one of the axes, just reverse the xlim or ylim of the plot, and you don't need to worry about flipping or negating your data:

t <- seq(0,1,0.2)
plot(t,t,type="l", xaxt = 'n', yaxt = 'n', ylim = rev(range(t)))
lines(t,t^2,col="green")
lines(t,t^3,col="blue")
axis(3)
axis(2, at = pretty(t), labels = format(pretty(t), digits = 1), las = 1)

enter image description here

rawr
  • 20,481
  • 4
  • 44
  • 78
  • If we invert the values of y axis, the curve also should change, (it should change 180 degree, then flip left to right). Here only the values in y-axis are changed, not the curve!! – BhishanPoudel Jan 26 '16 at 23:57
  • 1
    @BhishanPoudel you said invert the y-axis, I assumed you only meant the y axis not the whole plot, see edits – rawr Jan 27 '16 at 02:46