Black Lives Matter. Please consider donating to Black Girls Code today.

Plotly in R, setting ranges on y-axis

Hi, I have a bar graph in R which is going to be used on powerbi. I want to set a maximum and a minimum to the Y axis. By getting the value entered by the user into ‘Maximum Height’ field or ‘Minimum Height’ field to modify the visualisation.

I set up these two field,
“yscalemaxin”:{
“displayName”: “Maximum Height”,
“displayNameKey”: “dynymax”,
“type”: {
“numeric”: true
}
},
“yscaleminin”:{
“displayName”: “Minimum Height”,
“displayNameKey”: “dynymin”,
“type”: {
“numeric”: true

layout(
xaxis = list(range = c(0, yscalemaxin)),
yaxis = list(range = c(yscaleminin, Inf)))
this code do not work.
How can I get their values and then the chart changes? I think this function is same as ‘start’ and ‘end’ in PowerBI.

Greetings

Are you trying to do this in a Shiny App or through just the view window?

Hi, I am trying to do this through the view window

Hey @IceRoom

Plotly has custom controls (dropdowns, buttons, and sliders) that you can use to update the layout. For example:

library(plotly)

x <- c(1,2,3,4)
y <- c(1,2,3,4)
df <- data.frame(x,y)

p <- plot_ly(df, x = ~x, y = ~y, alpha = 0.3, height = 600, width = 900) %>%
  add_markers() %>%
  layout(
    title = "Use dropdowns to change yaxis range",
    updatemenus = list(
      list(
        y = 0.6,
        buttons = list(
          list(method = "relayout",
               args = list("yaxis.range[0]", 0),
               label = "0"),
          
          list(method = "relayout",
               args = list("yaxis.range[0]", -1),
               label = "-1"),
          
          list(method = "relayout",
               args = list("yaxis.range[0]", -2),
               label = "-2"))),
      list(
        y = 0.725,
        buttons = list(
          list(method = "relayout",
               args = list("yaxis.range[1]", 4),
               label = "4"),
          
          list(method = "relayout",
               args = list("yaxis.range[1]", 5),
               label = "5"),
          
          list(method = "relayout",
               args = list("yaxis.range[1]", 6),
               label = "6")))
    ),
    annotations = list(
      list(
        text='<b>ymin:',
        xref='paper',
        yref='paper',
        x='-0.11',
        y='0.625',
        showarrow=F
      ),
      list(
        text='<b>ymax:',
        xref='paper',
        yref='paper',
        x='-0.11',
        y='0.76',
        showarrow=F
      )
    ))