通常,当我们绘图时,绘图的底部(从左到右)有x轴,左边有y轴(从下到上).
例如,在R编程中,我有一个这样的代码:
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
Run Code Online (Sandbox Code Playgroud)
现在,如果我们想要绘图使得x轴在顶部(从左到右)和y轴反转(从上到下).
我们怎样才能在R编程中实现这样的壮举?我在stackoverflow中搜索了以下链接,但是它们无法满足我的要求:
如何在绘图上反转y轴
查看 ?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)))
Run Code Online (Sandbox Code Playgroud)
我不确定为什么.0会掉落y,但你可以labels = format(rev(pretty(t)), digits = 1)用来保持一致性
编辑
要反转其中一个轴的整个图,只需反转图xlim或ylim图,您不必担心翻转或否定数据:
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)
Run Code Online (Sandbox Code Playgroud)