首页 > 解决方案 > seq.int(from, to, length.out = n) 中的错误:'from' 必须是一个有限的数字,任何人都可以帮助我吗?

问题描述

这是我的代码,我同时使用 ggplot2 和 reconplots:

library(ggplot2)
demanda<- function(q) (100-q/10)
oferta<- function(q) (q/4)
x_range <- 1:500
curve_intersection<- curve(demanda, oferta, empirical=FALSE,
                           domain=c(min(x_range),max(x_range)))
curve_intersection
ggplot() +
    stat_function(aes(x_range)),color= "green",size=1,fun=demanda) +
    stat_function(aes(x_range)),color= "red",size=1,fun=oferta) +
    geom_vline(xintercept =curve_intersection$x,linetype= "doted" ) +
    geom_hline(yintercept = curve_intersection$y,linetype="doted") +
    theme_classic()

标签: r

解决方案


我通过他们的GitHubreconPlots对包进行了研究,这是我的解决方案

安装并加载包

library(devtools)
install_github("andrewheiss/reconPlots")
library(reconPlots)

我试图从他们的 GitHub 上学习这个例子,发现这个例子不起作用。所以我修复了一些代码,结果如下

library(ggplot2)
library(reconPlots)

demanda <- function(q) (100-q/10)
oferta <- function(q) (q/4)
x_range <- 1:500
curve_intersection <- curve_intersect(demanda, oferta, empirical=FALSE, domain=c(min(x_range),max(x_range)))

ggplot(data.frame(x_range), aes(x_range)) +
  stat_function(color= "green", size=1, fun = demanda) +
  stat_function(color= "red", size=1, fun = oferta) +
  geom_vline(xintercept = curve_intersection$x, linetype = "dotted") +
  geom_hline(yintercept = curve_intersection$y, linetype = "dotted") +
  theme_classic()

请注意,该curve_intersect函数来自reconPlots包,它与Rcurve中的包中的函数不同graphics


推荐阅读