Search code examples
rmirrorheatmapaxes

R: mirror y-axis from a plot


I have this problem. I got a heatmap, (but i suppose this applies to every plot) but I need to mirror my y-axis.

I got here some example code:

library(gstat)
x <- seq(1,50,length=50)
y <- seq(1,50,length=50)
z <- rnorm(1000)
df <- data.frame(x=x,y=y,z=z)
image(df,col=heat.colors(256)) 

This will generate the following heatmap First heatmap But I need the y-axis mirrored. Starting with 0 on the top and 50 on the bottom. Does anybody has a clue as to what I must do to change this? mirrored heatmap


Solution

  • See the help page for ?plot.default, which specifies

    xlim: the x limits (x1, x2) of the plot. Note that ‘x1 > x2’ is allowed and leads to a ‘reversed axis’.

    library(gstat)
    x <- seq(1,50,length=50)
    y <- seq(1,50,length=50)
    z <- rnorm(1000)
    df <- data.frame(x=x,y=y,z=z)
    

    So

    image(df,col=heat.colors(256), ylim = rev(range(y)))