如何解决 r plot_ly 子图的损坏的双 y-axis 图输出?
How to address broken dual y-axis plot output from r plot_ly subplotting?
我正在使用 plot_ly 库中的 subplot
函数在同一个“页面”上绘制 4 个 plot_ly 图。此处的数据是一个快速模型,用于表示我正在使用的数据集。我有 4 个解释变量族(即实际值 1-10,偏差 25,50,70,100),它们的系数,以及它们在虚拟模型中出现的归一化次数,以及用于填充向量的 NA 值,以便 data-frames:
require(tidyverse)
require(data.table)
require(stringr)
require(plotly)
require(processx)
Actuals <-c(1,2,3,4,5,6,7,8,9,10)
set.seed(10)
UniqCoef1 <- runif(10, min=-1, max=1)
NrmPolct1 <- c(.015,.005,.33,.32,.225,.025,.03,.05,NA ,NA)
dataframe1 <- data.frame(NrmPolct1,UniqCoef1,Actuals)
bias <-c(25,50,100,200)
set.seed(4)
UniqCoef2 <- runif(4, min=.08, max=.4)
NrmPolct2 <- c(.25,.5,NA,NA)
dataframe2 <- data.frame(NrmPolct2,UniqCoef2,bias)
cat <-c(1)
set.seed(1)
UniqCoef3 <- runif(1, min=-2, max=2)
NrmPolct3 <- c(.67)
dataframe3 <- data.frame(NrmPolct3,UniqCoef3,cat)
cvglvl <-c(55,60,65,70,75,80,85)
set.seed(7)
UniqCoef4 <- runif(7, min = -.5, max = .5)
NrmPolct4 <- c(.22,.33,.15,.05,.12,NA,NA)
dataframe4 <- data.frame(NrmPolct4,UniqCoef4,cvglvl)
为了固定它们各自的 y 轴和 y2 轴的范围,我根据系数的最大值构建了比例因子:
sclfctr1 <- ((.33*1.02)/.20)
sclfctr2 <- ((.5*1.02)/.20)
sclfctr3 <- ((.67*1.02)/.20)
sclfctr4 <- ((.33*1.02)/.20)
然后存储每个变量图:
plot1 <- plot_ly(dataframe1) %>%
add_trace(x=~Actuals, y=~UniqCoef1, type="scatter", mode="markers", name="Coef Values") %>%
add_bars(x=~Actuals, y=~NrmPolct1, yaxis="y2", name="% Polcy Count") %>%
layout(plot_bgcolor='#D0CFC9',
yaxis=list(scaleanchor="x", scaleratio=5,title="Coefficients", zeroline = FALSE,
range=c((((max(UniqCoef1)*1.02)-(max(UniqCoef1)*1.02)+(min(UniqCoef1)*.98))/.80),max(UniqCoef1)*1.02),
tickvals=UniqCoef1,standoff=30),
yaxis2=list(scaleacnchor="y", scaleratio=.15,overlaying="y",side="right", tickvals=NrmPolct1,range=c(0,sclfctr1),title="% Polcy Count", standoff=30),
xaxis=list(tickvals=Actuals,title=paste("actuals"),showgrid=T, standoff=30),
title=paste0("# Actuals for State 23, Crop 41."))
plot2 <- plot_ly(dataframe2) %>%
add_trace(x=~bias, y=~UniqCoef2, type="scatter", mode="markers", name="Coef Values") %>%
add_bars(x=~bias, y=~NrmPolct2, yaxis="y2", name="% Polcy Count") %>%
layout(plot_bgcolor='#D0CFC9',
yaxis=list(scaleanchor="x", scaleratio=5,title="Coefficients", zeroline = FALSE,
range=c((((max(UniqCoef2)*1.02)-(max(UniqCoef2)*1.02)+(min(UniqCoef2)*.98))/.80),max(UniqCoef2)*1.02),
tickvals=UniqCoef2,standoff=30),
yaxis2=list(scaleacnchor="y", scaleratio=.15,overlaying="y",side="right", tickvals=NrmPolct2,range=c(0,sclfctr2),title="% Polcy Count", standoff=30),
xaxis=list(tickvals=bias,title=paste("bias"),showgrid=T, standoff=30),
title=paste0("# bias for State 23, Crop 41."))
plot3 <- plot_ly(dataframe3) %>%
add_trace(x=~cat, y=~UniqCoef3, type="scatter", mode="markers", name="Coef Values") %>%
add_bars(x=~cat, y=~NrmPolct3, yaxis="y2", name="% Polcy Count") %>%
layout(plot_bgcolor='#D0CFC9',
yaxis=list(scaleanchor="x", scaleratio=5,title="Coefficients", zeroline = FALSE,
range=c((((max(UniqCoef3)*1.02)-(max(UniqCoef3)*1.02)+(min(UniqCoef3)*.98))/.80),max(UniqCoef3)*1.02),
tickvals=UniqCoef3,standoff=30),
yaxis2=list(scaleacnchor="y", scaleratio=.15,overlaying="y",side="right", tickvals=NrmPolct3,range=c(0,sclfctr3),title="% Polcy Count", standoff=30),
xaxis=list(tickvals=cat,title=paste("cat"),showgrid=T, standoff=30),
title=paste0("# cat for State 23, Crop 41."))
plot4 <- plot_ly(dataframe4) %>%
add_trace(x=~cvglvl, y=~UniqCoef4, type="scatter", mode="markers", name="Coef Values") %>%
add_bars(x=~cvglvl, y=~NrmPolct4, yaxis="y2", name="% Polcy Count") %>%
layout(plot_bgcolor='#D0CFC9',
yaxis=list(scaleanchor="x", scaleratio=5,title="Coefficients", zeroline = FALSE,
range=c((((max(UniqCoef4)*1.02)-(max(UniqCoef4)*1.02)+(min(UniqCoef4)*.98))/.80),max(UniqCoef4)*1.02),
tickvals=UniqCoef4,standoff=30),
yaxis2=list(scaleacnchor="y", scaleratio=.15,overlaying="y",side="right", tickvals=NrmPolct4,range=c(0,sclfctr4),title="% Polcy Count", standoff=30),
xaxis=list(tickvals=cvglvl,title=paste("cvglvl"),showgrid=T, standoff=30),
title=paste0("# cvglvl for State 23, Crop 41."))
其输出虽然不理想,也不是这个问题的重点(尽管任何关于如何限制 y2 占据绘图底部 15% 和 y 顶部 85% 的输入都将非常感激),但可行:
htmlwidgets::saveWidget(plot1, paste0('Number of actuals for State 23, Crop 41.html'))
plot1
htmlwidgets::saveWidget(plot2, paste0('Number of bias for State 23, Crop 41.html'))
plot2
htmlwidgets::saveWidget(plot3, paste0('Number of cat for State 23, Crop 41.html'))
plot3
htmlwidgets::saveWidget(plot4, paste0('Number of cvglvl for State 23, Crop 41.html'))
plot4
陷入混乱,没有标题、缺少条形图或散点图,以及遮挡现有数据的压缩范围:
Plot <- subplot(plot1, plot2, plot3, plot4, nrows=2) %>%
layout(plot_bgcolor='#e5ecf6',
xaxis = list(
zerolinecolor = '#ffff',
zerolinewidth = 2,
gridcolor = 'ffff'),
yaxis = list(
zerolinecolor = '#ffff',
zerolinewidth = 2,
gridcolor = 'ffff'))
htmlwidgets::saveWidget(Plot, paste0('Number of actuals-cvglvl for State 23, Crop 41.html'))
gridplot
我需要包括哪些参数以及我需要在哪里包括它们,以便 4 个奇异图在 2x2 网格中显示为它们各自实例的几乎相同的副本?
编辑 1:使用 annotations
实用程序允许图形和轴标签的包含和方向,但格式仍然无用。
编辑 2:在 GitHub 上发布了带有解决方法的已知错误。 Link 在下面的回答中。留下问题,以便其他人在遇到此问题时可以快速找到解决方法。
事实证明这是一个已知错误,在 GitHub 此处发布了解决方法:
https://github.com/plotly/plotly.R/issues/954#issuecomment-453872899
我正在使用 plot_ly 库中的 subplot
函数在同一个“页面”上绘制 4 个 plot_ly 图。此处的数据是一个快速模型,用于表示我正在使用的数据集。我有 4 个解释变量族(即实际值 1-10,偏差 25,50,70,100),它们的系数,以及它们在虚拟模型中出现的归一化次数,以及用于填充向量的 NA 值,以便 data-frames:
require(tidyverse)
require(data.table)
require(stringr)
require(plotly)
require(processx)
Actuals <-c(1,2,3,4,5,6,7,8,9,10)
set.seed(10)
UniqCoef1 <- runif(10, min=-1, max=1)
NrmPolct1 <- c(.015,.005,.33,.32,.225,.025,.03,.05,NA ,NA)
dataframe1 <- data.frame(NrmPolct1,UniqCoef1,Actuals)
bias <-c(25,50,100,200)
set.seed(4)
UniqCoef2 <- runif(4, min=.08, max=.4)
NrmPolct2 <- c(.25,.5,NA,NA)
dataframe2 <- data.frame(NrmPolct2,UniqCoef2,bias)
cat <-c(1)
set.seed(1)
UniqCoef3 <- runif(1, min=-2, max=2)
NrmPolct3 <- c(.67)
dataframe3 <- data.frame(NrmPolct3,UniqCoef3,cat)
cvglvl <-c(55,60,65,70,75,80,85)
set.seed(7)
UniqCoef4 <- runif(7, min = -.5, max = .5)
NrmPolct4 <- c(.22,.33,.15,.05,.12,NA,NA)
dataframe4 <- data.frame(NrmPolct4,UniqCoef4,cvglvl)
为了固定它们各自的 y 轴和 y2 轴的范围,我根据系数的最大值构建了比例因子:
sclfctr1 <- ((.33*1.02)/.20)
sclfctr2 <- ((.5*1.02)/.20)
sclfctr3 <- ((.67*1.02)/.20)
sclfctr4 <- ((.33*1.02)/.20)
然后存储每个变量图:
plot1 <- plot_ly(dataframe1) %>%
add_trace(x=~Actuals, y=~UniqCoef1, type="scatter", mode="markers", name="Coef Values") %>%
add_bars(x=~Actuals, y=~NrmPolct1, yaxis="y2", name="% Polcy Count") %>%
layout(plot_bgcolor='#D0CFC9',
yaxis=list(scaleanchor="x", scaleratio=5,title="Coefficients", zeroline = FALSE,
range=c((((max(UniqCoef1)*1.02)-(max(UniqCoef1)*1.02)+(min(UniqCoef1)*.98))/.80),max(UniqCoef1)*1.02),
tickvals=UniqCoef1,standoff=30),
yaxis2=list(scaleacnchor="y", scaleratio=.15,overlaying="y",side="right", tickvals=NrmPolct1,range=c(0,sclfctr1),title="% Polcy Count", standoff=30),
xaxis=list(tickvals=Actuals,title=paste("actuals"),showgrid=T, standoff=30),
title=paste0("# Actuals for State 23, Crop 41."))
plot2 <- plot_ly(dataframe2) %>%
add_trace(x=~bias, y=~UniqCoef2, type="scatter", mode="markers", name="Coef Values") %>%
add_bars(x=~bias, y=~NrmPolct2, yaxis="y2", name="% Polcy Count") %>%
layout(plot_bgcolor='#D0CFC9',
yaxis=list(scaleanchor="x", scaleratio=5,title="Coefficients", zeroline = FALSE,
range=c((((max(UniqCoef2)*1.02)-(max(UniqCoef2)*1.02)+(min(UniqCoef2)*.98))/.80),max(UniqCoef2)*1.02),
tickvals=UniqCoef2,standoff=30),
yaxis2=list(scaleacnchor="y", scaleratio=.15,overlaying="y",side="right", tickvals=NrmPolct2,range=c(0,sclfctr2),title="% Polcy Count", standoff=30),
xaxis=list(tickvals=bias,title=paste("bias"),showgrid=T, standoff=30),
title=paste0("# bias for State 23, Crop 41."))
plot3 <- plot_ly(dataframe3) %>%
add_trace(x=~cat, y=~UniqCoef3, type="scatter", mode="markers", name="Coef Values") %>%
add_bars(x=~cat, y=~NrmPolct3, yaxis="y2", name="% Polcy Count") %>%
layout(plot_bgcolor='#D0CFC9',
yaxis=list(scaleanchor="x", scaleratio=5,title="Coefficients", zeroline = FALSE,
range=c((((max(UniqCoef3)*1.02)-(max(UniqCoef3)*1.02)+(min(UniqCoef3)*.98))/.80),max(UniqCoef3)*1.02),
tickvals=UniqCoef3,standoff=30),
yaxis2=list(scaleacnchor="y", scaleratio=.15,overlaying="y",side="right", tickvals=NrmPolct3,range=c(0,sclfctr3),title="% Polcy Count", standoff=30),
xaxis=list(tickvals=cat,title=paste("cat"),showgrid=T, standoff=30),
title=paste0("# cat for State 23, Crop 41."))
plot4 <- plot_ly(dataframe4) %>%
add_trace(x=~cvglvl, y=~UniqCoef4, type="scatter", mode="markers", name="Coef Values") %>%
add_bars(x=~cvglvl, y=~NrmPolct4, yaxis="y2", name="% Polcy Count") %>%
layout(plot_bgcolor='#D0CFC9',
yaxis=list(scaleanchor="x", scaleratio=5,title="Coefficients", zeroline = FALSE,
range=c((((max(UniqCoef4)*1.02)-(max(UniqCoef4)*1.02)+(min(UniqCoef4)*.98))/.80),max(UniqCoef4)*1.02),
tickvals=UniqCoef4,standoff=30),
yaxis2=list(scaleacnchor="y", scaleratio=.15,overlaying="y",side="right", tickvals=NrmPolct4,range=c(0,sclfctr4),title="% Polcy Count", standoff=30),
xaxis=list(tickvals=cvglvl,title=paste("cvglvl"),showgrid=T, standoff=30),
title=paste0("# cvglvl for State 23, Crop 41."))
其输出虽然不理想,也不是这个问题的重点(尽管任何关于如何限制 y2 占据绘图底部 15% 和 y 顶部 85% 的输入都将非常感激),但可行:
htmlwidgets::saveWidget(plot1, paste0('Number of actuals for State 23, Crop 41.html'))
plot1
htmlwidgets::saveWidget(plot2, paste0('Number of bias for State 23, Crop 41.html'))
plot2
htmlwidgets::saveWidget(plot3, paste0('Number of cat for State 23, Crop 41.html'))
plot3
htmlwidgets::saveWidget(plot4, paste0('Number of cvglvl for State 23, Crop 41.html'))
plot4
陷入混乱,没有标题、缺少条形图或散点图,以及遮挡现有数据的压缩范围:
Plot <- subplot(plot1, plot2, plot3, plot4, nrows=2) %>%
layout(plot_bgcolor='#e5ecf6',
xaxis = list(
zerolinecolor = '#ffff',
zerolinewidth = 2,
gridcolor = 'ffff'),
yaxis = list(
zerolinecolor = '#ffff',
zerolinewidth = 2,
gridcolor = 'ffff'))
htmlwidgets::saveWidget(Plot, paste0('Number of actuals-cvglvl for State 23, Crop 41.html'))
gridplot
我需要包括哪些参数以及我需要在哪里包括它们,以便 4 个奇异图在 2x2 网格中显示为它们各自实例的几乎相同的副本?
编辑 1:使用 annotations
实用程序允许图形和轴标签的包含和方向,但格式仍然无用。
编辑 2:在 GitHub 上发布了带有解决方法的已知错误。 Link 在下面的回答中。留下问题,以便其他人在遇到此问题时可以快速找到解决方法。
事实证明这是一个已知错误,在 GitHub 此处发布了解决方法:
https://github.com/plotly/plotly.R/issues/954#issuecomment-453872899