File: plotly_ellipse.R

package info (click to toggle)
r-cran-mixtools 2.0.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,944 kB
  • sloc: ansic: 781; makefile: 6
file content (82 lines) | stat: -rw-r--r-- 2,748 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
plotly_ellipse <- function(mu, sigma, alpha=.05, npoints=250,
                           draw=TRUE,
                           cex = 3,
                           col = "#1f77b4",
                           lwd = 3,
                           title = "",
                           title.x = 0.5,
                           title.y = 0.95,
                           title.size = 15,
                           xlab = "X",
                           xlab.size = 15,
                           xtick.size = 15,
                           ylab = "Y",
                           ylab.size = 15,
                           ytick.size = 15
) {
  es <- eigen(sigma)
  e1 <- es$vec%*%diag(sqrt(es$val))
  r1 <- sqrt(qchisq(1-alpha,2))
  theta <- seq(0,2*pi,len=npoints)
  v1 <- cbind(r1*cos(theta),r1*sin(theta))
  pts=t(mu-(e1%*%t(v1)))
  
  updatemenus <- list(
    list(
      x = 1.2,
      y = 1,
      visible = TRUE,
      font = list(size=12),
      active = 0,
      type= 'dropdown',
      buttons = list(
        list(
          label = "Show Markers",
          method = "update",
          args = list(list(visible = c(TRUE, FALSE , FALSE)))),
        list(
          label = "Show Lines",
          method = "update",
          args = list(list(visible = c(FALSE , TRUE , FALSE)))),
        list(
          label = "Show Lines+Markers",
          method = "update",
          args = list(list(visible = c(FALSE , FALSE , TRUE)))))
    )
  )
  
  plot <- plot_ly() %>%
    add_markers(x=pts[,1] , 
                y=pts[,2] ,
                type = 'scatter' , mode = 'markers' ,
                marker = list(color = col , size = cex) , 
                name = 'Data' , showlegend = FALSE) %>%
    add_trace(x=pts[,1] , 
              y=pts[,2] , type = 'scatter' , mode = 'lines',
              line = list(color = col , width = lwd),
              name = "Data" , showlegend = FALSE) %>%
    add_trace(x=pts[,1] , 
              y=pts[,2] , type = 'scatter' , mode = 'lines+markers',
              marker = list(color = col , size = cex),
              line = list(color = col , width = lwd),
              name = "Data" , showlegend = FALSE) %>%
    plotly::layout(
      updatemenus = updatemenus,
      title = list(text = title,
                   x = title.x,
                   y = title.y,
                   font = list(size=title.size)
      ),
      xaxis = list(title = list(text = xlab,
                                font = list(size = xlab.size)),
                   tickfont = list(size = xtick.size)
      ),
      yaxis = list(title = list(text = ylab,
                                font = list(size = ylab.size)),
                   tickfont = list(size = ytick.size)
      )
    )
  print(plot)
  
  invisible(pts)
}