2015-11-25 84 views
0

我熟悉polygonggplot2,但我喜歡使用基本的plot有沒有一種方便的方法來填充R中的曲線?

我要填寫由命令調用曲線:

curve(dnorm(x, 0.5, 0.22), xlim=c(-0.5, 1.5))

enter image description here

當在過去,我已經使用polygon我所定義的終點基於沿x事先計算例如x <- seq(-0.5, 0.5, len = 100)的行,和y <- dnorm(x, 0.5, 0.22)。接下來,將限制內polygon定義,顏色選擇等瞧......在不到兩星期,有顏色...

OK,不完全是快,但在這種特殊情況下,我沒有甚至明確定義xy以外curve,這使得整個過程更加繁瑣。這幾乎就像重新開始。

有什麼辦法可以快速做到這一點,或許有fill,colcurve內的其他繪圖參數?

+0

如果您不打算從頭開始使用'polygon',那麼我認爲您需要修改'curve'的代碼。 –

+0

沒有訴諸ggplot2的選擇嗎?我真的不喜歡它的美學......我不是指在ggplot2中使用這個詞。 – Toni

+0

我當然沒有提到使用ggplot方法。下面的答案是我期待的。 –

回答

3

實際上可以相對容易地修改curve函數。只有我所做的改變是:

1)添加fill參數默認爲「紅」

2)在函數結束(標有註釋)

添加polygon陰謀這是實際的功能。請參閱下面的功能代碼。

my_curve(dnorm(x, 0.5, 0.22), xlim=c(-0.5, 1.5), fill="green") 

enter image description here

我不知道,這是任何更有利的是隻是在做:

x=seq(-0.5,1.5,0.01) 
plot(x, dnorm(x,0.5,0.22), xlim=c(-0.5,1.5), type="l") 
polygon(x, dnorm(x,0.5,0.22), col="green") 

但如果你打算使用它了很多,可以輸出my_curve,或使用curve的新版本創建一個包含基本版本的軟件包。您也可以使填充可選,並可能添加允許部分填充的功能。

如果你不是野生的GGPLOT2默認值,你也可以擺脫他們,同時仍保持ggplot的語法的好處:

library(ggplot2) 

df = data.frame(x=seq(-0.5,1.5,0.01), y=dnorm(x,0.5,0.22)) 

ggplot(df, aes(x,y)) + 
    geom_area(colour="black", fill="red") + 
    theme_bw() + 
    theme(panel.grid=element_blank()) 

下面是修改後的curve功能:

my_curve = function (expr, from = NULL, to = NULL, n = 101, add = FALSE, 
        type = "l", xname = "x", xlab = xname, ylab = NULL, log = NULL, 
        xlim = NULL, fill="red", ...) 
{ 
    sexpr <- substitute(expr) 
    if (is.name(sexpr)) { 
    expr <- call(as.character(sexpr), as.name(xname)) 
    } 
    else { 
    if (!((is.call(sexpr) || is.expression(sexpr)) && xname %in% 
      all.vars(sexpr))) 
     stop(gettextf("'expr' must be a function, or a call or an expression containing '%s'", 
        xname), domain = NA) 
    expr <- sexpr 
    } 
    if (dev.cur() == 1L && !identical(add, FALSE)) { 
    warning("'add' will be ignored as there is no existing plot") 
    add <- FALSE 
    } 
    addF <- identical(add, FALSE) 
    if (is.null(ylab)) 
    ylab <- deparse(expr) 
    if (is.null(from) || is.null(to)) { 
    xl <- if (!is.null(xlim)) 
     xlim 
    else if (!addF) { 
     pu <- par("usr")[1L:2L] 
     if (par("xaxs") == "r") 
     pu <- extendrange(pu, f = -1/27) 
     if (par("xlog")) 
     10^pu 
     else pu 
    } 
    else c(0, 1) 
    if (is.null(from)) 
     from <- xl[1L] 
    if (is.null(to)) 
     to <- xl[2L] 
    } 
    lg <- if (length(log)) 
    log 
    else if (!addF && par("xlog")) 
    "x" 
    else "" 
    if (length(lg) == 0) 
    lg <- "" 
    if (grepl("x", lg, fixed = TRUE)) { 
    if (from <= 0 || to <= 0) 
     stop("'from' and 'to' must be > 0 with log=\"x\"") 
    x <- exp(seq.int(log(from), log(to), length.out = n)) 
    } 
    else x <- seq.int(from, to, length.out = n) 
    ll <- list(x = x) 
    names(ll) <- xname 
    y <- eval(expr, envir = ll, enclos = parent.frame()) 
    if (length(y) != length(x)) 
    stop("'expr' did not evaluate to an object of length 'n'") 
    if (isTRUE(add)) 
    lines(x = x, y = y, type = type, ...) 
    else plot(x = x, y = y, type = type, xlab = xlab, ylab = ylab, 
      xlim = xlim, log = lg, ...) 
     polygon(x,y, col=fill) # Add filled area under curve 
    invisible(list(x = x, y = y)) 
} 
+0

優秀!謝謝! – Toni

相關問題