【问题标题】:Display only positive values of axis in plotly chart在绘图图中仅显示轴的正值
【发布时间】:2022-01-06 22:50:54
【问题描述】:

在下面的图表中,我只想显示 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

【问题讨论】:

    标签: r plotly


    【解决方案1】:

    您可以使用rangemodeyaxis 参数中实现您想要的结果。

    根据documentation

    范围模式
    父级:layout.yaxis
    类型:枚举,(“正常”|“tozero”|“非负”)之一
    默认值:“正常” 如果“正常”,则根据输入数据的极值计算范围。如果“tozero”`,范围扩展到 0,不管输入数据如果“nonnegative”,范围是非负的,不管输入数据。仅适用于线性轴。

    您需要将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
    

    这是输出:

    【讨论】:

      猜你喜欢
      • 2022-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-01
      • 2016-03-13
      • 1970-01-01
      相关资源
      最近更新 更多