gpt4 book ai didi

r - 如何在 R 编程中绘制顶部的 x 轴和倒置的 y 轴?

转载 作者:行者123 更新时间:2023-12-01 22:25:49 26 4
gpt4 key购买 nike

通常,当我们进行绘图时,绘图的 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

现在,如果我们想要绘制 x 轴在上(从左到右)和 y 轴倒置(从上到下)的图。
我们如何才能在 R 编程中实现这样的壮举?我在 stackoverflow 中搜索了以下链接,但它们无法满足我的要求:
How to invert the y-axis on a plot

Invert y-axis in barplot

最佳答案

查看 ?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

我不确定为什么 .0 被丢弃在 y 上,但您可以使用 labels = format(rev(pretty(t)), digits = 1)为了一致性

编辑

要围绕其中一个轴反转整个图,只需反转图的xlimylim,而您不需要需要担心翻转或否定您的数据:

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

关于r - 如何在 R 编程中绘制顶部的 x 轴和倒置的 y 轴?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34959436/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com