r - R中威布尔曲线下的着色子区域
问题描述
但不知何故,我无法使用stat_function
. 我得到argument missing
错误。添加args=list(..)
不起作用。
limitRange <- function(fun, min, max) {
function(x) {
y <- fun(x)
y[x < min | x > max] <- NA
return(y)
}
}
ggplot(data.frame(x=c(0,3)), aes(x)) +
stat_function(fun = dweibull,
args = list(shape = 2, scale = 1.12), alpha = .8, size = 1.1) + # works
stat_function(fun = limitRange(dweibull(shape = 2, scale = 1.12), 0.0297, 0.1189),
args = list(shape = 2, scale = 1.12), #doesn't work
geom = "area", fill = "blue", alpha = 0.2)
非常感谢任何帮助。
解决方案
你的问题是你打电话的方式limitRange
。它的第一个参数需要是一个函数,但是你给它dweibull(shape = 2, scale = 1.12)
,它不是一个函数。事实上,它是导致错误的原因:
dweibull(shape = 2, scale = 1.12)
# Error in dweibull(shape = 2, scale = 1.12) :
# argument "x" is missing, with no default
把它变成一个函数是可行的:
ggplot(data.frame(x = c(0, 2)), aes(x)) +
stat_function(fun = dweibull,
args = list(shape = 2, scale = 1.12)) +
stat_function(
fun = limitRange(function(z) dweibull(z, shape = 2, scale = 1.12), 0.0297, 0.1189),
geom = "area",
fill = "blue",
alpha = 0.2
)
一个整体更清洁的方法是给出limitRange
一个...
论点fun
:
limitRange <- function(fun, ..., min, max) {
return(function(x) {
y <- fun(x, ...)
y[x < min | x > max] <- NA
return(y)
})
}
ggplot(data.frame(x = c(0, 2)), aes(x)) +
stat_function(fun = dweibull,
args = list(shape = 2, scale = 1.12)) +
stat_function(
fun = limitRange(dweibull, shape = 2, scale = 1.12, min = 0.0297, max = 0.1189)
geom = "area",
fill = "blue",
alpha = 0.2
)
您将需要以这种方式命名min
和max
参数(如果fun
接受min
和max
参数可能会导致错误......更多唯一的名称可能会更好)。
推荐阅读
- linux - QProcess::用 su 命令执行
- javascript - AG-GRID 通过再次单击显示图标关闭迷你过滤器弹出窗口
- bash - 屏幕命令获取不到参数
- kubernetes - helm configmap 在 values.yaml 中包含客户端脚本
- django-filter - 如何使用 django_tables2 在同一个基于类的视图中定义两个或多个表
- mysql - 未能部署 Cloud Data Fusion“将数据从 MySQL 传输到 Google BigQuery”模板管道
- c# - 为来宾获取 Azure AD 令牌
- reactjs - AntD 表自定义过滤器功能不适用于 react 版本 15.4.2
- javascript - 一个intersectionObserver 观察多个元素或一个intersectionObserver 每个元素
- javascript - 在滚动时隐藏不透明度为 0.9 的固定 div