【问题标题】:Draw min, max function in R在 R 中绘制 min,max 函数
【发布时间】:2023-03-30 18:51:01
【问题描述】:

我在绘制最小值、最大值函数时遇到了困难 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")

到目前为止,我只在 22.27 得到一条水平线。

函数本身经过测试并在插入时返回正确的值,例如 eqs(50) = 26.57454

这是期望的结果应该是:

有人知道怎么做吗?

【问题讨论】:

  • 您需要使您的函数正确(当前缺少逗号)并通过args 参数将m 作为(命名)列表传递给tat_function
  • @RomanLuštrik:那没用。
  • 到目前为止您尝试过什么,结果在哪里?顺便说一句:你的问题有几个错别字。
  • 在原始帖子中查看我的更改。到目前为止,我只得到一条水平线。
  • 有人知道如何解决这个问题吗?

标签: r function ggplot2


【解决方案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")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-07
    • 2017-09-12
    • 2017-08-06
    • 2019-04-15
    • 2010-12-10
    • 2021-12-01
    • 1970-01-01
    • 2016-07-31
    相关资源
    最近更新 更多