在R中绘制min,max函数 [英] Draw min, max function in R
本文介绍了在R中绘制min,max函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在绘制min,max函数时遇到困难
1 /(min(max(c * x + d,1 / a),1 / b)))$
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 / 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的水平线。
函数本身经过测试并在插入时返回正确的值,例如eqs(50)= 26.57454 b
$ b 这是所需的结果应该是:
有人知道如何做到这一点吗?
解决方案
我猜问题em是该函数使用最大值和最小值,而不是pmax和pmin。
#加载软件包
library(ggplot2)
#定义参数
target
#定义函数
eqs = function(x){1 /(pmin(pmax(target [1,4] * x + target [1,5],1 / target [1,2])) ,1 / target [1,3]))}
#构造数据帧
df < - data.frame(x = c(0,10,20,30,40, ,50,55,60,70,100))
#绘制曲线
ggplot(df,aes(df $ x))+ stat_function(fun = eqs)+ xlab( x)+ ylab(y)
I am having difficulties drawing a min, max function
1/(min(max(c * x+d, 1/a),1/b)))
.
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")
I only get a horizontal line at 22.27 so far.
The function itself is tested and returns the correct values when inserted e.g., eqs(50) = 26.57454
This is the desired result should be:
Anybody an idea how to do it?
解决方案
I guess the problem was that the function was using max and min, instead of pmax and 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")
这篇关于在R中绘制min,max函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文