无法在 R 中的 ggplotly 条形图中设置 y 轴限制

Unable to set y axis limits in ggplotly bar chart in R

我在 R 中有以下数据集

 dataf<-data.frame("COl1"= c(5,10,15), "COl2"=c("A", "B", "C"))

我创建了以下带有扩展 Y 轴(范围在 -5、25 之间)的图,如下所示

  library(ggplot2) 
  library(plotly)
  pl1 <- ggplot(data = dataf, aes(x = COl2,y = COl1 , fill = COl2)) + 
            coord_cartesian(ylim = c(-5,25)) + 
            geom_bar(stat = 'identity', width = .35)

这会生成一个 barplot,其 y 轴介于 -5、25 之间。接下来,当我尝试使 dynamicticks 为真时,绘图范围会折叠到数据中可用的范围

ggplotly(pl1, dynamicTicks = T, layerData = T)

有没有办法在使用 ggplotly 命令

生成 dynamicticks 时保留展开的 Y 轴

我已经尝试了所有其他方法:coord_cartesianylim 等,但我无法让它工作

您可以在 plotly::layout 中为 yaxis 指定 autorange 参数。

library(dplyr)
library(ggplot2)
library(plotly)

dataf<-data.frame("COl1"= c(5,10,15), "COl2"=c("A", "B", "C"))

pl1<-ggplot(data = dataf, aes(x = COl2,y = COl1 , fill=COl2))+coord_cartesian(ylim = c(-5,25))+geom_bar(stat = 'identity', width = .35)

ggplotly(pl1, dynamicTicks = T, layerData = T) %>% 
  layout(yaxis = list(autorange = FALSE))

按照 this link 参考 R plotly。