本節書摘來異步社群《r資料可視化手冊》一書中的第2章,第2.6節,作者:【美】winston chang,更多章節内容可以通路雲栖社群“異步社群”公衆号檢視。
問題
如何繪制函數圖像?
方法
可以使用curve()函數繪制函數圖像,如圖2-12左圖所示。使用時需向其傳遞一個關于變量x的表達式:
myfun <- function(xvar) {
1/(1 + exp(-xvar + 10))
}
curve(myfun(x), from=0, to=20)
curve(1-myfun(x), add = true, col ="red")
library(ggplot2)
qplot(c(0, 20), fun=myfun, stat="function", geom="line")
ggplot(data.frame(x=c(0, 20)), aes(x=x)) + stat_function(fun=myfun, geom="line")