Skip to content

Instantly share code, notes, and snippets.

@mlaib
Created April 6, 2020 08:13
Show Gist options
  • Save mlaib/c1de01450f1573a082ee61b25664c1d0 to your computer and use it in GitHub Desktop.
Save mlaib/c1de01450f1573a082ee61b25664c1d0 to your computer and use it in GitHub Desktop.
# Suggestion plots using plotly library ####
# library(MFDFA)
# mfdfa<-MFDFA(tsx, scale, m, q) # mfdfa: contains all outputs
library(plotly)
# Supplementary functions ####
poly_fit<-function(x,y,n){
formule<-lm(as.formula(paste('y~',paste('I(x^',1:n,')', sep='',collapse='+'))))
res1<-coef(formule)
poly.res<-res1[length(res1):1]
allres<-list(polyfit=poly.res, model1=formule)
return(allres)}
# plots ####
resdata <- data.frame(x = scale, log2(mfdfa$Fqi), mfdfa$line)
colnames(resdata) <- c("Scale", paste("q", 1:length(q), sep=""),paste("Lq", 1:length(q), sep=""))
p1 <- plot_ly(resdata, x = ~Scale) %>%
add_trace(y = ~q1, mode = 'markers', type = 'scatter', color = I('orange'), showlegend=FALSE) %>%
add_trace(y = ~q13, mode = 'markers', type = 'scatter', color = I('#1f77b4'), showlegend = FALSE) %>%
add_trace(y = ~q21, mode = 'markers', type = 'scatter', color = I('#bcbd22'), showlegend = FALSE) %>%
add_trace(y = ~Lq1, name = 'q = -10', type = 'scatter', mode = 'lines', color = I('orange')) %>%
add_trace(y = ~Lq13, name = 'q = 2', type = 'scatter', mode = 'lines', color = I('#1f77b4')) %>%
add_trace(y = ~Lq21, name = 'q = 10', type = 'scatter', mode = 'lines', color = I('#bcbd22')) %>%
layout(xaxis = list(type = "log", title="scale"), yaxis=list(title="log<sub>2</sub>(F<sub>q</sub>)"))
p2 <- plot_ly(x = q) %>%
add_trace(y=mfdfa$Hq, mode = 'markers', type = 'scatter', color = I('black'), showlegend=FALSE) %>%
layout(xaxis=list(zeroline = FALSE, title="q"), yaxis=list(title="H<sub>q</sub>",
range=c(0, ifelse(max(mfdfa$Hq)>=1, max(mfdfa$Hq), 1))))
p3 <- plot_ly(x = q) %>%
add_trace(y=mfdfa$tau_q, mode = 'markers', type = 'scatter', color = I('black'), showlegend=FALSE) %>%
layout(xaxis=list(zeroline = FALSE, title="q"), yaxis=list(zeroline = FALSE, title= "T<sub>q</sub>"))
x1=mfdfa$spec$hq
y1=mfdfa$spec$Dq
rr<-poly_fit(x1,y1,4)
mm1<-rr$model1
mm<-rr$polyfit
x2<-seq(0,max(x1)+1,0.01)
curv<-mm[1]*x2^4+mm[2]*x2^3+mm[3]*x2^2+mm[4]*x2+mm[5]
p4 <- plot_ly(x = mfdfa$spec$hq) %>%
add_trace(y=mfdfa$spec$Dq, mode = 'markers', type = 'scatter', color = I('black'), showlegend=FALSE) %>%
add_trace(x =x2, y = curv, type = 'scatter', mode = 'lines', color = I('#e377c2'), showlegend=FALSE) %>%
layout(xaxis=list(zeroline = FALSE, title="a", range=c(min(mfdfa$spec$hq),max(mfdfa$spec$hq))),
yaxis=list(zeroline = FALSE, title="f(a)", range=c(min(mfdfa$spec$Dq),max(mfdfa$spec$Dq))))
p <- subplot(p1, p2, p3, p4, nrows = 2, margin = 0.07, titleY=TRUE, titleX=TRUE) %>%
layout(annotations = list(
list(x = 0.05, y = 1.0, font = list(size = 16), text = "(a)", xref = "paper", yref = "paper",
xanchor = "right", yanchor = "bottom", showarrow = FALSE),
list( x = 0.6, y = 1.0, font = list(size = 16), text = "(b)", xref = "paper", yref = "paper",
xanchor = "center", yanchor = "bottom", showarrow = FALSE ),
list( x = 0.05, y = 0.4, font = list(size = 16), text = "(c)", xref = "paper", yref = "paper",
xanchor = "center", yanchor = "bottom", showarrow = FALSE ),
list( x = 0.6, y = 0.4, font = list(size = 16), text = "(d)", xref = "paper", yref = "paper",
xanchor = "center", yanchor = "bottom", showarrow = FALSE )))
p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment