【问题标题】:chart alignment in plotly bar chartplotly 条形图中的图表对齐方式
【发布时间】:2021-09-05 01:32:01
【问题描述】:

我的标题对齐在图表左侧不起作用。如何将标题左对齐。

我正在尝试在下面的图表中对齐标题,但对我不起作用请帮助我在这里缺少什么或我必须更新哪一行

df <- data.frame("LOC" =c("CA","NY","WA","TX"),
                 "TAX" = c(3421.00,5640.00,7880.32,4569.00))

ttl ="Tax collection"

g <- plot_ly(df, x =~TAX, y = ~ LOC, type = 'bar',width = 1200, height =900, showlegend=F
             , text = ~paste0(roundUp(TAX*100), "%"), textposition = "outside",
             marker = list(color = "blue"))


tit <- list(
  text = ttl,
  font = cht_ttl,
  xref = "paper",
  yref = "paper",
  xanchor = "right",
  x = 0.1,
  y = 1,
  showarrow = FALSE
)
ax <- list(
  title = "",
  showgrid = FALSE,
  range = c(0, 1)
)

chart <- g %>% layout(annotations = tit,
                      xaxis = ax,
                      yaxis = list (title = " "),font=chrt_title, showline = TRUE)

chart

【问题讨论】:

  • 请包含您正在使用的非基础包。 roundUp 函数来自哪个包?
  • roundup是自定义取整,刚刚更新
  • @RonakShah 所以在我的示例中,Tx 集合应该在左上角,但它在图表上方。

标签: r plotly bar-chart


【解决方案1】:

使用xanchor = 'left'x 值小于 0。

library(plotly)

tit <- list(
  text = ttl,
  font = chrt_title,
  xref = "paper",
  yref = "paper",
  xanchor = "left",
  x = -0.04,
  y = 1,
  showarrow = FALSE
)


chart <- g %>% layout(annotations = tit,
                      xaxis = ax,
                      yaxis = list (title = " "),
                      font=chrt_title, showline = TRUE)

chart

【讨论】:

  • 这个我已经在我的代码中了,所以税收应该对齐左上角,在 WA legent 上方,它现在在条形图上方
  • 标题已经在左上角。我不明白你想要它在哪里?
  • 您可以调整x 值。我已经更新了答案以显示x = -0.04 的外观。您可以根据需要进一步减少。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-25
  • 1970-01-01
  • 2021-12-28
  • 2022-01-21
  • 1970-01-01
  • 2017-10-24
  • 1970-01-01
相关资源
最近更新 更多