【问题标题】:Control which tick marks / labels appear on x-axis in plotly?控制哪些刻度线/标签出现在 x 轴上?
【发布时间】:2016-10-15 01:28:48
【问题描述】:

我想控制 x 轴上出现的刻度线。以下代码以 5 的顺序放置刻度线(在 5、10、15 ... 30)

library(plotly)

df <- data.frame(x =  1:30,
                 y = sample(100:300, size = 30, replace = T))

p <- plot_ly(data = df, x = x, y = y, type = 'line') %>%
      layout(title = 'Example plot')
p

我需要将它们按 6、12、18、24、30 的顺序排列。我一直在浏览文档,但似乎找不到我需要的东西。在ggplot2 中,这可以通过scale_x_continuous(breaks=c(6,12,18,24,30) 完成。

【问题讨论】:

    标签: r plotly r-plotly


    【解决方案1】:

    您可以使用style 添加刻度:

    p <- plot_ly(data = df, x = x, y = y, type = 'line') %>%
        layout(title = 'Example plot', xaxis = list(autotick = F, dtick = 6))
    p
    

    这里有更多示例:https://plot.ly/r/axes/

    【讨论】:

      【解决方案2】:

      如果你需要不均匀的间距,你可以使用tickvals,它需要tickmode = "array",如下所示

      %>%
        layout(xaxis = list(autotick = F, tickmode = "array", tickvals = c(6,12,24)))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-14
        • 2020-02-22
        • 2011-03-28
        • 1970-01-01
        • 2019-12-18
        • 1970-01-01
        • 1970-01-01
        • 2019-02-08
        相关资源
        最近更新 更多