首页 > 解决方案 > 如何将更高和相等的子句分配给函数,而不会得到真假结果?输入

问题描述

library(ggplot2)

p <- ggplot(data = data.frame(x = 0), mapping = aes(x = x))

fun.1 <- function(x) 1000*x+3000*y

fun.1 >=300000*  **#this clause gives me error; if instead y run:strong text*

                                     *fun.1 <- function(x) 1000*x+3000*y )>=300000,* 

                                     **then I get true or false*

p + stat_function(fun = fun.1) + xlim(0,300)

标签: rfunctionggplot2

解决方案


第一部分建议是将您的功能更改为:

y = function(x) 1000 / 3000 * x

这个函数很容易解决,也简化了我们的逻辑。由于这很简单,我们也可以通过基本数学求解这个函数何时超过 300,000。

thresh = 300000
x_min = thresh / (1000 / 3000)
x_min
## [1] 9e+05

最后,我们可以更改绘图调用以将x_min用作 的一部分xlim

library(ggplot2)
p <- ggplot(data = data.frame(x = 0), mapping = aes(x = x))

p + stat_function(fun = y) + xlim(x_min,x_min + 300)


推荐阅读