我想绘制使用ggplot散点图下的等倾线,但我无法弄清楚如何正确使用stat_function
。R:ggplot距离公式
的等值线是基于距离公式:
sqrt((x1-x2)^2 + (y1-y2)^2)
和看起来像这些 同心圆,除了中心将是图的原点:
我到目前为止所尝试的是在ggplot内调用距离函数(注意:我使用x1 = 1和y1 = 1,因为在我真正的问题中,我也有固定值)
distance <- function(x, y) {sqrt((x - 1)^2 + (y - 1)^2)}
ggplot(my_data, aes(x, y))+
geom_point()+
stat_function(fun=distance)
但[R返回错误:
Computation failed in 'stat_function()': argument "y" is missing, with no default
如何正确喂养x和y的值stat_function
,以便它绘制的距离公式的一般情节,与原点的中心?
'stat_function'只能搞定计算ÿ功能。您需要将公式重新排列为函数y = f(x)。 – Roland