2016-07-07 78 views
-1

我在绘制min,max函数时遇到困难 1/(min(max(c * x+d, 1/a),1/b)))绘制min,max函数R

library(ggplot2) 
target <- data.frame(year = c(2011), a = c(29.82), b = c(22.27),c = c(0.0004546), d=c(0.014900)) 
eqs = function(x){1/(min(max(target[1,4] * x + target[1,5], 1/target[1,2]),1/target[1,3]))} 

ggplot(data.frame(x=c(0,10,20,30,40,50,55,60,70, 100)), aes(x)) + stat_function(fun=eqs) + xlab("x") + ylab("y") 

到目前为止,我只能得到22.27的水平线。

函数本身进行测试,并返回例如插入时正确的值,等式(50)= 26.57454

这是期望的结果应该是: Example outcome

任何人的想法如何做呢?

+0

你需要让你的函数正确的(目前还有一个失踪逗号),并通过'args'参数作为一个(命名)名单通过'M'到'tat_function'。 –

+0

@RomanLuštrik:这不会帮助。 –

+0

你到目前为止尝试过什么,结果在哪里?顺便说一句:你的问题有几个错别字。 – Uwe

回答

1

我想问题是函数使用的是max和min,而不是pmax和pmin。

# Loading the package 
library(ggplot2) 

# Defining the parameters 
target <- data.frame(year = c(2011), a = c(29.82), b = c(22.27),c = c(0.0004546), d=c(0.014900)) 

# Defining the function 
eqs = function(x){1/(pmin(pmax(target[1,4]*x+target[1,5],1/target[1,2]),1/target[1,3]))} 

# COnstructing the data frame 
df <- data.frame(x=c(0,10,20,30,40,50,55,60,70, 100)) 

# Ploting the curve 
ggplot(df, aes(df$x)) + stat_function(fun = eqs) + xlab("x") + ylab("y") 

enter image description here

+0

感谢迭戈,解决了这个问题! –