代码之家  ›  专栏  ›  技术社区  ›  Anakin Skywalker

使用特征多项式根在ggplot2中绘制单位圆

  •  0
  • Anakin Skywalker  · 技术社区  · 4 年前

    tidyverse

    我知道 this question ,它只是因为某些原因对我不起作用。

    > dput(my_df)
    c(0.492476485097794, 0, -0.0571584138399486, -0.348306694268216, 
    0.510825623765991, -0.0512932943875506, -0.0540672212702757, 
    -0.325422400434628, 0.526093095896779, 0, -0.0465200156348928, 
    -0.336472236621213, 0.550046336919272, 0, -0.0800427076735366, 
    -0.287682072451781, 0.51082562376599, 0, -0.0689928714869512, 
    -0.287682072451781, 0.481838086892739, 0, -0.060624621816435, 
    -0.287682072451781, 0.432864082296279, 0, -0.0555698511548109, 
    -0.336472236621213, 0.470003629245736, 0.0246926125903714, 
    -0.075985906977922, 
    -0.305381649551182, 0.517943091534855, 0, -0.0434851119397388, 
    -0.31015492830384, 0.473784352085642, -0.0190481949706944, 
    -0.0392207131532814, 
    -0.2484613592985, 0.413975797776073, 0.0168071183163813, 0, 
    -0.22314355131421, 
     0.362905493689368, 0.0143887374520996, -0.0143887374520996, 
    -0.191055236762709, 
     0.375789339962048, -0.0121213605323448, -0.0500104205746613, 
    -0.152016207298626, 0.370018359112417, -0.0421114853501268, 
    -0.0666913744986723, 
    -0.175448677506193, 0.304660408986199, -0.010152371464018, 0, 
    -0.190518323998133, 0.359141036433926, -0.0996298409488412, 
    0.00947874395454378, 
    -0.186102279633861, 0.358945092473272, -0.0655972824858133, 
    -0.00851068966790863, 
    -0.218879152527752, 0.292987124681474, -0.091349778588228, 0.042559614418796, 
    -0.162518929497775, 0.234839591077401, -0.064021858764931, 
    0.0163938097756764, 
    -0.177455367142782, 0.270545790208794)
    

    我在R基上做了一些工作,找到了特征多项式根并画出了圆。

    但我真的很想用 ggplot2 ,只是不知道如何。

    我的基本R代码是:

    library(plotrix)
    
    gdpDiff <- diff(my_df)
    m1 = ar(gdpDiff, method = 'mle')
    m1$order
    m1$x.mean
    m1$ar
    p1 = c(1, -m1$ar)
    r1 = polyroot(p1) 
    r1
    r1Re <- Re(r1)
    r1Im <- Im(r1)
    Mod(r1)
    plot(r1Re, r1Im, asp = 1, xlim = c(min(r1Re), max(r1Re)), ylim = c(min(r1Im), max(r1Im)))
    draw.circle(0, 0, radius = 1)
    abline(v = 0)
    abline(h = 0)
    

    我目前的结果是(4点):

    0 回复  |  直到 4 年前
        1
  •  1
  •   Dhiraj    4 年前

    ggplot2 ,包裹 ggforce 有功能( geom_circle )用来做圆圈的。你可以用这个。我编写了一个函数,将实部和虚部转换为TIBLE,然后您可以使用它进一步绘图:

    library(ggplot2)
    library(ggforce)
    library(plotrix)
    
    poly_fun <- function(my_vector) {
      gdpDiff <- diff(my_vector)
      m1 <- ar(gdpDiff, method = 'mle')
      p1 <- c(1, -m1$ar)
      r1 <- polyroot(p1) 
      tibble(
        real = Re(r1),
        imaginary = Im(r1)
      )
    }
    df <- poly_fun(my_df) 
    
        
      ggplot() +
      geom_circle(data = tibble(x = 0, y = 0), aes(x0 = x, y0 = y, r = 1)) +
        geom_point(data = df, aes(x = real, y = imaginary)) +
        xlim(min(df$real), max(df$real)) +
        ylim(min(df$imaginary), max(df$imaginary)) +
        coord_fixed(ratio = 1) +
        geom_hline(yintercept = 0) +
        geom_vline(xintercept = 0)