Formatting Ticks in R

How to format axes ticks in R.


Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Sign up for early access now.

Tickmode - Linear

library(plotly)

fig <- plot_ly(
  type = "scatter",
  x = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), 
  y = c(28.8, 28.5, 37, 56.8, 69.7, 79.7, 78.5, 77.8, 74.1, 62.6, 45.3, 39.9), 
  mode = "markers+lines") 
fig <- fig %>%
  layout(
    xaxis = list(
      dtick = 0.75, 
      tick0 = 0.5, 
      tickmode = "linear"
  ))

fig
Click to copy
0.51.2522.753.54.2555.756.57.2588.759.510.251111.7512.5304050607080

Tickmode - Array

library(plotly)

fig <- plot_ly(
  type = "scatter",
  x = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), 
  y = c(28.8, 28.5, 37, 56.8, 69.7, 79.7, 78.5, 77.8, 74.1, 62.6, 45.3, 39.9), 
  mode = "markers+lines") 
fig <- fig %>%
  layout(
    xaxis = list(
      ticktext = list("One", "Three", "Five", "Seven", "Nine", "Eleven"), 
      tickvals = list(1, 3, 5, 7, 9, 11),
      tickmode = "array"
  ))

fig
Click to copy

Using Tickformat

library(plotly)

fig <- plot_ly(
  type = "scatter",
  x = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), 
  y = c(0.18, 0.38, 0.56, 0.46, 0.59, 0.4, 0.78, 0.77, 0.74, 0.42, 0.45, 0.39), 
  mode = "markers+lines") 
fig <- fig %>%
  layout(
    yaxis = list(
        tickformat = "%"
  ))

fig
Click to copy

Using Tickformat (Date)

library(plotly)

df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')

fig <- plot_ly(
  type = "scatter",
  x = df$Date, 
  y = df$AAPL.High,
  name = 'AAPL High',
  mode = "lines",
  line = list(
        color = '#17BECF'
  )) 
fig <- fig %>%
  add_trace(
    type = "scatter",
    x = df$Date, 
    y = df$AAPL.Low,
    name = 'AAPL Low',
    mode = "lines",
    line = list(
        color = '#7F7F7F'
  )) 
fig <- fig %>%
  layout(
    title = "Time Series with Custom Date-Time Format",
    xaxis = list(
        type = 'date',
        tickformat = "%d %B (%a)<br>%Y"
  ))

fig
Click to copy
01 July (Wed)201501 January (Fri)201601 July (Fri)201601 January (Sun)201790100110120130
AAPL HighAAPL LowTime Series with Custom Date-Time Format

Tickformatstops to customize for different zoom levels

library(plotly)
df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
fig <- plot_ly(
  type = "scatter",
  x = df$Date, 
  y = df$mavg,
  mode = "lines") 
fig <- fig %>%
  layout(
    xaxis = list(
      type='date',
      tickformatstops = list(
        list(
          dtickrange = list(NULL, 1000), 
          value = "%H:%M:%S.%L ms"
        ), 
        list(
          dtickrange = list(1000, 60000), 
          value = "%H:%M:%S s"
        ), 
        list(
          dtickrange = list(60000, 3600000), 
          value = "%H:%M m"
        ), 
        list(
          dtickrange = list(3600000, 86400000), 
          value = "%H:%M h"
        ), 
        list(
          dtickrange = list(86400000, 604800000), 
          value = "%e. %b d"
        ), 
        list(
          dtickrange = list(604800000, "M1"), 
          value = "%e. %b w"
        ), 
        list(
          dtickrange = list("M1", "M12"), 
          value = "%b '%y M"
        ), 
        list(
          dtickrange = list("M12", NULL), 
          value = "%Y Y"
        )
      )
    )
  )

fig
Click to copy
Apr '15 MJul '15 MOct '15 MJan '16 MApr '16 MJul '16 MOct '16 MJan '17 M95100105110115120125130

Using Exponentformat

library(plotly)

fig <- plot_ly(
  type = "scatter",
  x = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), 
  y = c(68000, 52000, 60000, 20000, 95000, 40000, 60000, 79000, 74000, 42000, 20000, 90000),
  mode = "markers+lines") 
fig <- fig %>%
  layout(
    yaxis = list(
      showexponent = "all",
      exponentformat = "e"
    )
  )

fig
Click to copy
246810120.2e+50.3e+50.4e+50.5e+50.6e+50.7e+50.8e+50.9e+51e+5

What About Dash?

Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.

Learn about how to install Dash for R at https://dashr.plot.ly/installation.

Everywhere in this page that you see fig, you can display the same figure in a Dash for R application by passing it to the figure argument of the Graph component from the built-in dashCoreComponents package like this:

library(plotly)

fig <- plot_ly() 
# fig <- fig %>% add_trace( ... )
# fig <- fig %>% layout( ... ) 

library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)

app <- Dash$new()
app$layout(
    htmlDiv(
        list(
            dccGraph(figure=fig) 
        )
     )
)

app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE)
Click to copy