Is there any way to set a spacing between axis labels and axis ticks labels other than changing margins?
Example plot:
plot_ly(mtcars, x = ~ wt, y = ~ pa
Since plotly v4.9.2 you can use the new standoff layout attribute which
Sets the standoff distance (in px) between the axis labels and the title text The default value is a function of the axis tick labels, the title
font.size
and the axislinewidth
. Note that the axis title position is always constrained within the margins, so the actual standoff distance is always less than the set or default value. By settingstandoff
and turning onautomargin
, plotly.js will push the margins to fit the axis title at given standoff distance.
Example:
plotly::plot_ly(mtcars, x = ~ wt, y = ~ paste0(hp, '0000')) %>%
plotly::layout(yaxis = list(title = list(text = "Gross horsepower",
standoff = 40L)))
Increasing the left margin as @Codutie suggests in his answer does help to a certain degree only (at least in plotly v4.7.1+). Most of the 150 pixel margin on the left is wasted space as can be seen in the screenshot below:
R-code to generate the above plot (from @Codutie):
library(plotly)
plot_ly(mtcars, x = ~ wt, y = ~ paste0(hp, '0000')) %>%
layout(margin = list(l = 150, r = 20, b = 10, t = 10))
Due to Plotly's goal of being responsive, it does a lot of the layout scaling automatically – which generally is a good thing IMHO because you don't have to fiddle around too much to get a presentable chart. Of course the results of Plotly's autoscaling aren't always perfect or exactly what you want to achieve and that's why there are plenty of dedicated (layout) attributes letting you manipulate certain aspects of your plot. Unfortunately there's no attribute to set the distance between an axis label and the axis itself (or the axis tick labels).
Instead you can rely on the following workaround to manipulate the distance between the axis and it's label by setting an appropriate y-axis title string:
plot_ly(mtcars, x = ~ wt, y = ~ paste0(hp, '0000')) %>%
layout(margin = list(l = 150, r = 20, b = 10, t = 10),
yaxis = list(title = paste0(c(rep(" ", 20),
"Gross horsepower",
rep(" ", 20),
rep("\n ", 3)),
collapse = "")))
This results in:
Explanation of the y-axis title components:
rep("\n ", 3)
: This is the main part of the trick to increase the distance – just suffix the actual title ("Gross horsepower"
) with newlines and non-breaking spaces.rep(" ", 20)
: This is an additional measure to ensure Plotly's autoscaling doesn't interfere again – wrapping the actual title in 40 non-breaking spaces (could be more/less of course). Otherwise the text Gross horsepower would be placed in between two axis ticks as soon as the plot height gets large enough to "physically" allow it – which would kinda foil our attempt to manually set the distance because we would have to use a much bigger newline-non-breaking-space combo to get the same distance again and – more importantly – the distance would depend on the plot height. In the above plot this isn't really necessary though, because there are as much y-axis ticks that you would need to display the plot with a height way beyond 1000px to reach that critical point.