Raghavan vmvs
Raghavan vmvs

Reputation: 1265

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

I have the following dataset in R

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

I have created the following plot with an expanded Y axis (range between -5, 25) as follows

  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)

This produces a barplot with a y axis that is between -5, 25. Next when I try to make dynamicticks true the plot range collapses to the range available in the data

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

Is there a way to retain the expanded Y axis while generating dynamicticks using ggplotly command

I have tried all other methods: coord_cartesian, ylim etc but am unable to get it to work

Upvotes: 1

Views: 860

Answers (1)

Croote
Croote

Reputation: 1424

You can specify the autorange argument for yaxis in plotly::layout.

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))

Follow this link to the reference for R plotly.

Upvotes: 3

Related Questions