[英]How can I reverse the y-axis in a raster plot?
I would like to plot a raster with the y-axis increasing from top to bottom, and the raster correspondingly flipped.我想绘制一个 y 轴从上到下增加的栅格,并且栅格相应地翻转。 Is there a way to do this?有没有办法做到这一点? Specifying ylim
in decreasing order gives an error.以降序指定ylim
会产生错误。
For example, I would like this plot with the y-axis running from -100 at the top to 100 at the bottom, with the raster also reversed to put green on top and orange on the bottom:例如,我希望这个图的 y 轴从顶部的 -100 到底部的 100,栅格也反转以将绿色置于顶部,橙色置于底部:
library(raster)
r <- raster(nrows=10, ncols=10)
r <- setValues(r, 1:ncell(r))
plot(r)
# Fails
plot(r, ylim=c(100, -100))
#> Error in .plotraster2(x, col = col, maxpixels = maxpixels, add = add, : invalid ylim
Created on 2020-10-30 by the reprex package (v0.3.0)由reprex 包(v0.3.0) 于 2020 年 10 月 30 日创建
The approach shown below appears to work下面显示的方法似乎有效
Example data示例数据
library(raster)
r <- raster(nrows=10, ncols=10, vals=1:100)
Flip the raster vertically, and add the horizontal axis垂直翻转光栅,并添加水平轴
plot(flip(r, "y"), axes=F)
axis(1)
Get the labels for the vertical axis, and plot them in reverse order获取垂直轴的标签,并以相反的顺序绘制它们
ylabs <- axis(2, labels=FALSE, tick=FALSE)
axis(2, at=ylabs, labels=rev(ylabs))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.