[英]How do I keep the plotted graph and yaxis aligned when doing stacked subplots with plotly R
我正在嘗試用 plotly R 生成一堆圖,但是相對於零軸的繪制線正在漂移,因此頂部圖中的零在底部圖中不為零。 任何想法如何解決這一問題?
假設上面的圖是正確的(它是:高斯隨機游走在零附近)。 然后注意,在 2:4 的圖中,從上到下,x 軸向上漂移,但所有圖都使用了完全相同的數據。 請注意 y 軸上的數字 2 向上漂移。 我覺得這是一個簡單的問題(非常基本 - 使值與數據保持一致。)所以我認為我遺漏了一些明顯的東西,所以如果有人能指出我明顯的錯誤,我將不勝感激。
我試過設置anchor
和scaleanchor
但這似乎對定位沒有影響,即使position = 0
和anchor = 'free'
也是如此。
我也嘗試過固定范圍和autorange
范圍的變fixedrange
,但還是沒有快樂。
這是一個可重現的例子:
set.seed(2244)
cols <- c('black','red','green','cyan','blue','magenta','yellow','gray')
outlist <- list()
nplots <- c(1,2,3,4)
bounds <- 0
trials <- 0
M <- 1
N <- 50
i <- 1
y <- rnorm(N)
chleaf <- rbinom(N, 1, 0.5)
outmat <- matrix(0, nrow=N, ncol=6)
outmat[,c(1,3)] <- rnorm(dim(outmat)[1]*2, 0, 1)
outmat[,c(2,4)] <- outmat[,c(1,3)]^2
outmat[,c(5,6)] <- y - outmat[,c(1,3)]
for (j in nplots) {
mgrid <- NULL
if ( bounds == 1 ) {
mgrid <- c(min(outmat[,3]-2*sqrt(outmat[,4]))-0.5,
max(outmat[,3]+2*sqrt(outmat[,4]))+0.5)
} else {
mgrid <- c(min(min(outmat[,3]), min(outmat[,5]), min(y))-0.5,
max(max(outmat[,3]), max(outmat[,5]), max(y))+0.5)
}
outlist[[i]] <- plotly::plot_ly() %>%
plotly::add_trace(x = 1:(M*N), y = outmat[,3], type = 'scatter', mode='lines',
showlegend=ifelse(i==1, TRUE, FALSE), name=TeX('\\mu_{t|t-1}'),
line=list(color=cols[1], width=0.5)) %>%
plotly::add_trace(x = 1:(M*N), y = outmat[,3]+2*sqrt(outmat[,4]),
type='scatter', mode='markers', color=I(cols[1]), size=0.5,
showlegend=ifelse(i==1, TRUE, FALSE), name=TeX('\\Sigma_{t|t-1}'),
marker=list(symbol='cross-thin'), visible=ifelse(bounds==1, TRUE, FALSE)) %>%
plotly::add_trace(x = 1:(M*N), y = outmat[,3]-2*sqrt(outmat[,4]),
type='scatter', mode='markers', color=I(cols[1]), size=0.5,
showlegend=FALSE, marker=list(symbol='cross-thin'),
visible=ifelse(bounds==1, TRUE, FALSE)) %>%
plotly::add_trace(x = 1:(M*N), y = outmat[,1], type = 'scatter', mode='lines',
showlegend=ifelse(i==1, TRUE, FALSE), name=TeX('\\mu_{t|t}'),
line=list(color=cols[2], width=0.5)) %>%
plotly::add_trace(x = 1:(M*N), y = outmat[,1]+2*sqrt(outmat[,2]),
type='scatter', mode='markers', color=I(cols[2]), size=0.5,
showlegend=ifelse(i==1, TRUE, FALSE), name=TeX('\\Sigma_{t|t}'),
marker=list(symbol='cross-thin'), visible=ifelse(bounds==1, TRUE, FALSE)) %>%
plotly::add_trace(x = 1:(M*N), y = outmat[,1]-2*sqrt(outmat[,2]),
type='scatter', mode='markers', color=I(cols[2]), size=I(5),
showlegend=FALSE, marker=list(symbol='cross-thin'),
visible=ifelse(bounds==1, TRUE, FALSE)) %>%
plotly::add_trace(x = 1:(M*N), y = outmat[,5], type = 'scatter', mode='lines',
showlegend=ifelse(i==1, TRUE, FALSE), name=TeX('\\hat{y}_{t}'),
line=list(color=cols[3], width=0.5)) %>%
plotly::add_trace(x = 1:(M*N), y = outmat[,6], type = 'scatter', mode='lines',
showlegend=ifelse(i==1, TRUE, FALSE), name=TeX('\\tilde{y}_{t}'),
line=list(color=cols[4], width=0.5)) %>%
plotly::config(mathjax='cdn') %>%
plotly::layout(
xaxis=list(title=list(text='Iterations', standoff=0),
showline=T, showgrid=F, range = c(0, ifelse(N==1, M+0.25, N*M+0.5)),
anchor='y', scaleanchor='x'),
yaxis=list(showline=T, showgrid=F, range=mgrid))
rMe <- 0
for (n in 1:N) {
rMe <- rMe+M
nchleaf <- (M*(n-1)+1):(n*M)*chleaf[(M*(n-1)+1):(n*M)]
xupdates <- nchleaf[which(nchleaf!=0)]
yupdates <- as.vector(sapply(y[n], function(x){rep(x,length(xupdates))}))
outlist[[i]] <- outlist[[i]] %>%
plotly::add_trace(x = c(M*(n-1), n*M), y = c(y[n], y[n]),
type = 'scatter', mode='lines',
showlegend=ifelse((i==1 && n==1), TRUE, FALSE), name='y',
line=list(color = cols[6], dash = ifelse(M==1, 'solid', 'dash'),
width=0.5)) %>%
plotly::add_trace(x = xupdates, y = yupdates,
type='scatter', mode='markers',
showlegend=ifelse((i==2&&n==1), TRUE, FALSE), name='Update',
color=I(cols[5]), size=0.5) %>%
plotly::add_trace(x = rMe, y = mgrid,
type = 'scatter', mode='lines', visible=ifelse(trials==1, TRUE, FALSE),
showlegend=ifelse((trials==1 && i==1 && n==1), TRUE, FALSE), name='Trial',
line=list(color = cols[8], dash = 'dash', width=0.5))
}
i <- i+1
}
fig <- plotly::subplot(outlist, nrows=length(nplots), shareX=TRUE,
which_layout=c(1)) %>%
plotly::config(staticPlot=T, mathjax='cdn', displayModeBar = F)
fig <- fig %>% plotly::layout(
showlegend=TRUE,
legend=list(itemsizing='trace', orientation='h', xanchor='center', x=0.5),
margin=list(b=70, l=45, r=30, t=80),
title=list(text="Test Title"))
# yaxis=list(autorange=TRUE, fixedrange=FALSE))
# xaxis=list(anchor='y', scaleanchor='x'),
# yaxis=list(anchor='x', scaleanchor='y'))
# yaxis=list(range=mgrid))#,
# xaxis=list(title=list(text='Iterations', standoff=0),
# showline=T, showgrid=F, range = c(0, ifelse(N==1, M+0.25, N*M+2))),
# yaxis=list(showline=T, showgrid=F))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.