R Plotly:如何将config()与plotly_build()结合使用?

Luk*_*ham 2 javascript plot r plotly

运行代码,包括plotly_build(p)正确的绘图结果.

可重复的代码

library(plotly)

#data
df1 <- data.frame(cond = factor( rep(c("A","B"), each=200) ),
                  rating = c(rnorm(200),rnorm(200, mean=.8)))

df2 <- data.frame(x=c(.5,1),cond=factor(c("A","B")))

#plot
gg <- ggplot(data=df1, aes(x=rating, fill=cond)) +
    geom_vline(aes(xintercept=mean(rating, na.rm=T))
               , color="red", linetype="dashed", size=1, name="average") +
    geom_vline(aes(xintercept=median(rating, na.rm=T))
               , color="blue", linetype="dashed", size=1, name="median", yaxt="n") +
    geom_histogram(binwidth=.5, position="dodge")

#create plotly object
p <- plotly_build(gg)

#append additional options to plot object
p$data[[1]]$hoverinfo <- "name+x" #hover options for 'average'
p$data[[2]]$hoverinfo <- "name+x" #hover options for 'median'

#display plot
plotly_build(p)
config(displayModeBar = F, showLink = F) # comment this line/config(.. out to get the plot
Run Code Online (Sandbox Code Playgroud)

问题

我想用来config改变一些设置.但是,使用config()似乎会覆盖这些hoverinfo变化.

在运行config之前(情节应该如何)......

在此输入图像描述

跑完后config(displayModeBar = F, showLink = F)......

在此输入图像描述

最后,我尝试在行之前运行config hoverinfo:

#create plotly object
p <- plotly_build(gg)
config(p=p,displayModeBar = F, showLink = F) #run config before 'hoverinfo' changes

#append additional options to plot object
p$data[[1]]$hoverinfo <- "name+x" #hover options for 'average'
p$data[[2]]$hoverinfo <- "name+x" #hover options for 'median'

#display plot
plotly_build(p)
Run Code Online (Sandbox Code Playgroud)

但是,config设置似乎被返回displayModeBar(下面的屏幕截图):

在此输入图像描述

Van*_*pez 5

添加最后一个配置行对我有用:

p <- plotly_build(gg)

p$data[[1]]$hoverinfo <- "name+x" #hover options for 'average'
p$data[[2]]$hoverinfo <- "name+x" #hover options for 'median'

p$config <- list(displayModeBar = F, showLink = F)
Run Code Online (Sandbox Code Playgroud)

源自源代码.

更新

截至至少Plotly版本4.5.6,config现在x是Plotly对象的属性的一部分.该行应为:

p$x$config <- list(displayModeBar = F, showLink = F)
Run Code Online (Sandbox Code Playgroud)