Axes in ggplot2
How to work with Axes in ggplot2 with Plotly.
Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Try Plotly Studio now.
Note: We are retiring documentation for R, MATLAB, Julia, and F# in November 2025. Learn more about this change here.
Default plot
library(plotly)
library(ggplot2)
p <- ggplot(mtcars) +
geom_point(aes(disp, mpg)) +
scale_x_continuous(name = "Displacement")
ggplotly(p)
Changing axis limits
library(plotly)
library(ggplot2)
p <- ggplot(mtcars) +
geom_point(aes(disp, mpg)) +
scale_x_continuous(limits = c(0, 600))
ggplotly(p)
Changing axis ticks
The breaks argument is used to specify where the ticks appear. It takes a numeric vector equal to the length of the number of ticks.
library(plotly)
library(ggplot2)
p <- ggplot(mtcars) +
geom_point(aes(disp, mpg)) +
scale_x_continuous(breaks = c(150, 300, 450))
ggplotly(p)
Changing tick labels
You can change the tick labels using the labels argument. In the below example, we use words instead of numbers. When adding labels, we need to ensure that the length of the breaks and labels are same.
library(plotly)
library(ggplot2)
p <- ggplot(mtcars) +
geom_point(aes(disp, mpg)) +
scale_x_continuous(breaks = c(150, 300, 450),
labels = c('One Hundred Fifty', 'Three Hundred', 'Four Hundred Fifity'))
ggplotly(p)
Changing position of labels
library(ggplot2)
p <- ggplot(mtcars) +
geom_point(aes(disp, mpg)) +
scale_x_continuous(position = 'top')
ggplotly(p)
Y-axis example
Everything above using scale_x_continuous() can be used in the same way for y axis with scale_y_continuous().
library(plotly)
library(ggplot2)
p <- ggplot(mtcars) + geom_point(aes(disp, mpg)) +
scale_y_continuous(name = "Miles Per Gallon", limits = c(0, 45),
breaks = c(0, 15, 30, 45), position = 'right',
labels = c('zero', 'fifteen', 'thirty', 'fourtyfive'))
ggplotly(p)
Discrete axis labels
library(plotly)
library(ggplot2)
p <- ggplot(mtcars) +
geom_bar(aes(factor(cyl))) +
scale_x_discrete(labels = c("4" = "Four", "6" = "Six", "8" = "Eight"))
ggplotly(p)
Remove Axis Labels
library(plotly)
library(ggplot2)
df <- diamonds[sample(1:nrow(diamonds), size = 1000),]
p <- ggplot(df, aes(carat, price, color = cut)) +
geom_point() +
theme(axis.text = element_blank())
ggplotly(p)
Vertical Text Orientation
library(plotly)
library(ggplot2)
lab <- paste("Vertical Label", c(1, 2, 3, 4, 5))
ds <- data.frame(x = sample(lab, size = 1000, replace = T),
y = sample(LETTERS[1:5], size = 1000, replace = T))
p <- ggplot(ds, aes(x = x, fill = y)) +
geom_bar() +
theme(axis.text.x = element_text(angle = 90)) +
ggtitle("Vertical Axis Labels")
ggplotly(p)
Angled Text Orientation
library(plotly)
library(ggplot2)
lab <- paste("Angle Label", c(1, 2, 3, 4, 5))
ds <- data.frame(x = sample(lab, size = 1000, replace = T),
y = sample(LETTERS[1:5], size = 1000, replace = T))
p <- ggplot(ds, aes(x = x, fill = y)) +
geom_bar() +
theme(axis.text.x = element_text(angle = 45)) +
ggtitle("Angle Axis Labels")
ggplotly(p)
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)