stat_function problem

Hi,

My issue is that I would like to use stat_function with a polynom, but I got a strange line. Especially interesting is that if I change the x axis limits, the y value change of the straight line.

I think there is a bug in the args argument, but I cant fix it.

Thank you for your help in advance
Reprex enclosed

library(tidyverse)
#> Warning: package 'tidyverse' was built under R version 3.6.3
#> Warning: package 'ggplot2' was built under R version 3.6.3
#> Warning: package 'tibble' was built under R version 3.6.3
#> Warning: package 'tidyr' was built under R version 3.6.3
#> Warning: package 'dplyr' was built under R version 3.6.3
#> Warning: package 'forcats' was built under R version 3.6.3

(A <- matrix(c(2,2,0,2,8,0,0,0,2), ncol = 3))
#>      [,1] [,2] [,3]
#> [1,]    2    2    0
#> [2,]    2    8    0
#> [3,]    0    0    2

plot_char <- function(matrix, limits = c(0, 15)) {
  
  v <- pracma::charpoly(matrix)
  y <- function(x, v) sum(v*rep(x, length(v))^c((length(v)-1):0))
  
  ggplot(data.frame(x = limits), aes(x)) +
    geom_hline(yintercept = 0, linetype = "dotted") +
    stat_function(fun = y, args = list(v = v)) +
    scale_x_continuous(expand = c(0, 0))
}

plot_char(A, c(0,500))

Created on 2020-08-31 by the reprex package (v0.3.0)

Marcell

y <- function(x, v)

x comes from where?

I want to use this function together with stat_function, as you can see below. If I force it by hand, the rigth values appear, if I write a function only with x input (but it is not possible now), it works. These two do not want to work together.

The argument for x is required to analyze the function.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.