Control which tick marks / labels appear on x-axis in plotly?

北城以北 提交于 2019-12-10 18:25:12

问题


I want to have control over which tick marks appear on the x-axis. The following code places tick marks in a sequence of 5 (at 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

I need to place them in a sequence of 6 at 6, 12, 18, 24, 30. I've been browsing the documentation but I cannot seem to find what I need. In ggplot2 this can be done via scale_x_continuous(breaks=c(6,12,18,24,30).


回答1:


You can use style to add the ticks:

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

Here are some more examples: https://plot.ly/r/axes/




回答2:


If you need uneven spacing you can use tickvals, it requires tickmode = "array" as below

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


来源:https://stackoverflow.com/questions/37809906/control-which-tick-marks-labels-appear-on-x-axis-in-plotly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!