0

I am trying to write the base R code, using tidyverse style.

I know this question, it just did not work for me for some reasons.

> 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)

I did some work in base R, finding characteristic polynomial roots and drawing the circle.

But I really want to do it, using ggplot2, just do not know how.

My base R code is:

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)

My current result is (with 4 dots):

Any chance to do the same, using ggplot? I would greatly appreciate your help.

Anakin Skywalker
  • 2,400
  • 5
  • 35
  • 63

1 Answers1

1

While this is not entirely with ggplot2, the package ggforce has a function (geom_circle) for making circles. You can use that. I have written a function to convert the real and imaginary parts into a tibble and you can then use it to plot further:

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) 

Dhiraj
  • 1,650
  • 1
  • 18
  • 44