Plotly R:设置轴标签和轴刻度标签之间的间距 [英] Plotly R: setting the spacing between axis label and axis ticks labels

查看:1154
本文介绍了Plotly R:设置轴标签和轴刻度标签之间的间距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了更改页边距外,是否有其他方法可以设置轴标签和轴刻度标签之间的间距?

Is there any way to set a spacing between axis labels and axis ticks labels other than changing margins?

示例图:

plot_ly(mtcars, x = ~ wt, y = ~ paste0(hp, '0000'))

如在示例图上方的y轴标题上方是该轴的重叠刻度标签.有没有办法在它们之间设置间距?

As in the example plot above the title of the y axis is overlapping tick labels of this axis. Is there a way to set a spacing between them?

推荐答案

增加左边界如@Codutie在他的回答中所建议的仅在某种程度上有所帮助(至少在Plotly v4.7.1 +中).左边的150像素空白大部分是浪费的空间,如下面的屏幕截图所示:

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代码以生成上面的图(来自@Codutie):

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))

由于Plotly的目标是响应,因此它会自动进行很多布局缩放–恕我直言,这通常是一件好事,因为您不必花太多时间就能得到一个合适的图表.当然,Plotly自动缩放的结果并不总是完美的,也不是您想要实现的,这就是为什么有很多东西的原因专用(布局)属性,让您可以操作绘图的某些方面. 很遗憾,没有属性可以设置轴标签和轴本身之间的距离(或轴刻度标签).

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).

相反,您可以依靠以下解决方法,通过设置适当的y轴标题字符串来操纵轴与其标签之间的距离:

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 = "")))

这导致:

y轴标题组件的说明:

Explanation of the y-axis title components:

  • rep("\n ", 3):这是增加距离的技巧的主要部分-只需在实际标题("Gross horsepower")后加上换行符和不间断空格即可.
  • rep(" ", 20):这是确保Plotly的自动缩放不会再次受到干扰的另一种措施-将实际标题包装在40个不间断的空格中(当然可能更多/更少).否则,一旦情节高度变得足够大以至于物理上"允许,文字 Gross power 就会被放置在两个轴刻度之间,这将挫败我们手动设置距离的尝试,因为我们会必须使用更大的换行符(不间断空格)组合键来再次获得相同的距离,更重要的是,该距离将取决于地块的高度.在上面的绘图中,这并不是真正必要的,因为您需要显示y轴刻度线的高度才能达到临界点,而显示高度超过1000px的绘图.
  • 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 big 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 achieve 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.

这篇关于Plotly R:设置轴标签和轴刻度标签之间的间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆