firmo23
firmo23

Reputation: 8404

Display only positive values of axis in plotly chart

In the plotly chart below I want to display only the positive values of the y-axis and not the negative.

df<-data.frame(
  Country<-"AL",
  Year<-2003,
  Scope<-0
)
library(plotly)
fig <- plot_ly(
  data = df,
  x = ~ Year,
  y = ~ Scope,
  type = "scatter",
  mode = "lines+markers",
  marker = list(
    size = 10,
    color = 'rgba(255, 182, 193, .9)',
    line = list(color = 'rgba(152, 0, 0, .8)',
                width = 2)
  ),
  line = list(color = 'rgba(152, 0, 0, .8)',
              width = 2)
) %>% layout(
  title = "Count of Scope per country and year",
  xaxis = list(
    dtick = 5
  ),
  yaxis = list(
    dtick = 1,
    tick0 = 0
  )
)

fig

Upvotes: 1

Views: 1498

Answers (1)

Vishal A.
Vishal A.

Reputation: 1381

You can achieve the result you are looking for using, rangemode inside yaxis parameters.

According to the documentation:

rangemode:
Parent: layout.yaxis
Type: enumerated , one of ( "normal" | "tozero" | "nonnegative" )
Default: "normal" If "normal", the range is computed in relation to the extrema of the input data. If "tozero"`, the range extends to 0, regardless of the input data If "nonnegative", the range is non-negative, regardless of the input data. Applies only to linear axes.

You'll need to set rangemode to nonnegative.

Here's the whole code:

df<-data.frame(
  Country<-"AL",
  Year<-2003,
  Scope<-0
)
library(plotly)
fig <- plot_ly(
  data = df,
  x = ~ Year,
  y = ~ Scope,
  type = "scatter",
  mode = "lines+markers",
  marker = list(
    size = 10,
    color = 'rgba(255, 182, 193, .9)',
    line = list(color = 'rgba(152, 0, 0, .8)',
                width = 2)
  ),
  line = list(color = 'rgba(152, 0, 0, .8)',
              width = 2)
) %>% layout(
  title = "Count of Scope per country and year",
  xaxis = list(
    dtick = 5
  ),
  yaxis = list(
    dtick = 1,
    tick0 = 0,
    rangemode = "nonnegative"
  )
)

fig

And here's the output: enter image description here

Upvotes: 2

Related Questions