-1

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: Example outcome

Anybody an idea how to do it?

Bert Maier
  • 61
  • 2
  • 11

1 Answers1

1

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

enter image description here

Diego Rodrigues
  • 824
  • 4
  • 10