如何在格子中添加双曲曲线

问题描述 投票:0回答:1

假设我想在该图中添加双曲曲线

data(cars)    
xyplot(dist ~ speed, cars)

尽管这样的函数无法拟合数据,但曲线应该像您在图片中看到的那样。您能给我建议正确的代码吗?

r curve lattice hyperbolic-function
1个回答
0
投票

我不清楚您要绘制的确切函数,但基本上您可以将各种组件添加为提供给

panel
xyplot
参数的函数:

xyplot(
    dist ~ speed,
    cars,
    panel = function(x, y, fun = sinh, line_scale = 5, text_scale = 1.2, from = 0, to = 25, ...) {
        panel.xyplot(x, y, col = "black")
        panel.lines(x = seq(from, to), y = mean(x), col = "red", lwd = 2)
        panel.text(min(x) * text_scale, mean(x) * text_scale, "mean", col = "red")
        panel.curve(
            fun(x / line_scale),
            from = from,
            to = to,
            type = "l",
            col = "blue",
            lwd = 3
        )
    }
)

© www.soinside.com 2019 - 2024. All rights reserved.