防止在条形图中切断长的x轴刻度标签,图中为R

kev*_*w22 9 plot r plotly

我试图使用plotly长条形图绘制条形图作为x轴标签.但是,这些字符串被截断,plotly如下所示:

Plotly chart轴

通过plotly轴的属性列表,我已经尝试过设置诸如tickangle(现在我意识到这一点没有意义)和其他几个,但都没有用.

Jot*_*ota 21

您可以在layout函数中调整图形布局中的边距.

可重复的例子,因为没有提供:

d <- data.frame(traitMean = apply(iris[-5], 2, mean))
# long labels
labs <- c("Long name for this", "Long name for that",
          "Long names everywhere", "Petal Width")
Run Code Online (Sandbox Code Playgroud)

如果使用默认边距绘制此标签,标签将被截止:

# example where ticklabels are cutoff
plot_ly(y = d[["traitMean"]], x = labs, type = "bar") %>% 
    layout(xaxis = list(tickangle = 45))
Run Code Online (Sandbox Code Playgroud)

您可以从margin参数中的默认值调整底部边距layout. margin获取一个命名列表,其中b是"底部"边距的名称.160 px适用于此示例,但您可能需要找到适用于您的标签的值.

plot_ly(y = d[["traitMean"]], x = labs, type = "bar") %>% 
    layout(margin = list(b = 160), xaxis = list(tickangle = 45))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 照着做?找到布局边距隐藏的位置.以下是你可以用它做的事情:[link](https://plot.ly/r/reference/#layout-margin) (2认同)

Abh*_*k R 5

textposition="outside". 为了避免这种情况,除了设置边距以修复 y 轴标签截断之外,还要设置cliponaxis = FALSE 来修复值标签截断。

以下是值标签截断的示例,尽管添加了顶部和底部边距以删除 y 轴标签截断:

library(plotly)

plot_ly(
x = c("1. Group 1", "2. Txn","3. AOV","4. Account/Recv CV","5. Cost %","6. Lost %","7. Take Rate","8. Group 2"),
  y = c(3.8,0,0,0,0,0,0,3.8),
  name = "SF Zoo",
  type = "waterfall",
  measure = c("relative", "relative", "relative", "relative", "relative", "relative", "relative","total"),
  text = c(3.8,0,0,0,0,0,0,3.8), textposition = 'outside'
) %>% 
layout(margin = list(b = 20,t=20))
Run Code Online (Sandbox Code Playgroud)

结果图的截断值为 3.8。 值标签被截断

当您添加时,cliponaxis = FALSE切断被删除

plot_ly(
  x = c("1. Group 1", 
        "2. Txn",
        "3. AOV",
        "4. Account/Recv CV",
        "5. Cost %",
        "6. Lost %", 
        "7. Take Rate",
        "8. Group 2"),
  y = c(3.8,0,0,0,0,0,0,3.8),
  name = "SF Zoo",
  type = "waterfall",
  measure = c("relative", "relative", "relative", "relative", "relative", "relative", "relative","total"),
  text = c(3.8,0,0,0,0,0,0,3.8), textposition = 'outside', cliponaxis = FALSE
) %>% 
layout(margin = list(b = 20,t=20))
Run Code Online (Sandbox Code Playgroud)

添加 Cliponaxis = FALSE 后删除截断

希望这可以帮助