Formatting Ticks in R
How to format axes ticks in R.
New to Plotly?
Plotly is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.
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
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
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
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
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
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
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)