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

时间:2015-12-17 06:46:09

标签: javascript r plot plotly

运行代码,包括Range("A78:CA91")和正确的绘图结果。

可重现代码

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 更改一些设置。但是,使用config似乎会覆盖config()更改。

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

enter image description here

然后运行hoverinfo ...

enter image description here

最后,我尝试在config(displayModeBar = F, showLink = F)行之前运行配置:

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) 设置似乎被config(下面的屏幕截图)的返回所覆盖:

enter image description here

2 个答案:

答案 0 :(得分:5)

添加最后的 config 行对我有用:

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)

源自源代码。

更新

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

p$x$config <- list(displayModeBar = F, showLink = F)

答案 1 :(得分:1)

您的代码无效的原因是您没有将config调用分配给p。 config无形地返回修改过的图形对象。

即,这应该可以正常工作:

p <- config(p=p,displayModeBar = F, showLink = F)