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