2014-09-30 95 views
2

當試圖用ggplot複製繪圖函數時遇到問題。我有兩個功能,定義如下:用ggplot2繪製「倒V」函數

GetMse <- function(x) 1 - max(x, 1 - x) 
GetGini <- function(x) 2 * x * (1 - x) 

使用默認R中繪製系統圖中看起來像:

figure-1

現在用ggplot出現問題:在圖紅色功能1是繪製爲直線上的圖2

enter image description here

任何IDE一個還是指針?下面粘貼ggplot代碼。

library(ggplot2) 
ggplot(data = data.frame(x = c(0.00001, 0.99999)), aes(x)) + 
    stat_function(fun = GetGini, geom = "line") + 
    stat_function(fun = GetMse, geom = "line", colour = "red") 

回答

4

GetGini返回一個矢量但GetMse返回一個標,通過使用pmax固定GetMse