我想繪製使用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