寫在前面。
很多時候在處理數據前或者出圖前,可能需要先對數據整體情況進行了解。這個時候我們可以用到R基礎繪圖的語句
和ggplot2
完成目標。
接下來,我們分不同的圖形類型
進行啃書學習。
6. 繪制函數圖像
如何繪制函數圖像?
- 使用R基礎繪圖系統
使用curve
函數繪制函數圖像,使用時向其中傳遞一個關于x
的表達式。
自建一個函數
myfun <- function(varx){
1/(1 + exp(-varx + 10))
}
使用curve
函數繪制圖像:
curve(myfun(x),from = 0 , to=20)
基礎繪圖系統繪制
使用add=TRUE
參數,可以向已有圖像添加圖像:
curve(1-myfun(x),add = TRUE , col= "red")
基礎繪圖系統繪制
- 使用ggplot2
> qplot(c(0,20) , fun=myfun, stat = "function", geom = 'line')
Error:
! The `stat` argument of `qplot()` was deprecated in ggplot2 2.0.0 and is
now defunct.
Run `rlang::last_trace()` to see where the error occurred.
qplot
語句已經棄用了,因此還是使用常用的ggplot2語句:
ggplot(data.frame(x = c(0,20)), aes(x = x)) + stat_function(fun = myfun, geom = "line")
ggplot2繪制
以上。