有没有一种方便的方法可以在 R 中对曲线进行颜色填充?

Is There an Expedient Way to Color-Fill a Curve in R?

我熟悉 polygonggplot2,但我喜欢使用基本的 plot

我要填写命令调用的曲线:

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

当我过去使用 polygon 时,我根据 x 的先前计算定义了终点,例如 x <- seq(-0.5, 0.5, len = 100)y <- dnorm(x, 0.5, 0.22).接下来,限制在 polygon 内定义,选择的颜色,等……不到两周,就有了颜色……

好吧,不是很快,但是在这个特殊情况下,我什至没有在curve之外显式定义xy,这使得整个过程更加繁琐.这几乎就像重新开始。

有什么方法可以快速做到这一点,也许可以使用 fillcolcurve 中的其他绘图参数?

您实际上可以相对轻松地修改 curve 函数。我所做的唯一更改是:

1) 添加一个fill参数默认为"red"

2) 在函数末尾添加一个polygon plot(用注释标记)

这是正在运行的函数。功能代码见下

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

我不确定这样做是否更方便:

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))
}