在图表中仅显示轴的正值

Display only positive values of axis in plotly chart

在下面的图表中,我只想显示 y 轴的正值而不是负值。

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

您可以在 yaxis 参数内使用 rangemode 来获得您想要的结果。

根据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.

您需要将 rangemode 设置为 nonnegative

完整代码如下:

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

这是输出: