【问题标题】:Unable to set y axis limits in ggplotly bar chart in R无法在 R 中的 ggplotly 条形图中设置 y 轴限制
【发布时间】:2019-12-09 05:25:31
【问题描述】:

我在 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 设为 true 时,绘图范围会折叠到数据中可用的范围

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

有没有办法在使用ggplotly命令生成dynamicticks时保留扩展的Y轴

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

【问题讨论】:

    标签: r ggplot2 plotly ggplotly


    【解决方案1】:

    您可以在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 链接到 R plotly 的参考。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-06
      • 1970-01-01
      • 2011-04-16
      相关资源
      最近更新 更多