gpt4 book ai didi

r - 用坐标轴和箭头绘制 R 中的函数

转载 作者:行者123 更新时间:2023-12-02 19:37:22 27 4
gpt4 key购买 nike

我是R的相对初学者,所以如果这是一个菜鸟问题,请原谅我。

那么,是否有一个包可以提供一个简单的界面来绘制(实数、数学)函数?我需要带有箭头的坐标轴(它们的交点应该是 (0;0))和刻度、网格等。我想要与 this document 中类似的图。 .

背景:现在我使用 LaTeX 的 tikzpictureaxis 创建函数图,但几个月以来我一直在使用 R 生成随机检查( R 创建 tex 文件并将它们包含到文档中),如果 R 可以创建类似的绘图(png、jpg),那就太好了,因为 < em>axis 在 LaTeX 中非常慢。

谢谢!

最佳答案

我为此为你做了一个小功能

math_plot <- function(f, xlim = c(-2,2), ylim = c(-2,2), 
xlab = "x", ylab = "f(x)", ax.ext = .02,
frame.plot = F, grid.tick = .1, ...){

curve(f, from = xlim[1], to = xlim[2], ylim = ylim,
axes = F, xlab = "", ylab = "",
frame.plot = frame.plot, ...)

# x-axis
axis(1, pos = 0)
arrows(x0 = xlim[2], x1 = xlim[2] + diff(xlim)*ax.ext, y0 = 0, length = .1)
mtext(text = xlab, side = 4, line = 0, las = 2, at = 0)

# y-axis
axis(2, pos = 0, las = 2)
arrows(y0 = ylim[2], y1 = ylim[2] + diff(ylim)*ax.ext, x0 = 0, length = .1)
mtext(text = ylab, side = 3, line = 0, at = 0)

grid(nx = diff(xlim)/grid.tick, ny = diff(ylim)/grid.tick)
}

# give it a function
math_plot(function(x) 3*x + 2 - 2*x^2, ylim = c(-2,4))

enter image description here

关于r - 用坐标轴和箭头绘制 R 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60882055/

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