我如何允许Plotly条形文本溢出条形?

问题描述 投票:1回答:1

如何获得dr5和dr3的文本大小的短条?如果文本长于条形图跨度,我希望文本溢出到条形图的末尾。

我尝试在布局中使用统一文本,但是这会将所有文本缩小为所使用的最小字体。如何将所有字体更改为使用的最大字体?

barchart

library(plotly)

# Test long text and short bars
xValues <- c("loooooooooooooooooonnnnnngg","lonnnnnnnnnnggggggg",
             "dr3","dr4","dr5")
yValues <- c(0.5,1,2,0.22,10)

bar <- plot_ly(x = yValues,
               y = xValues) %>% 
       add_trace(
               type = 'bar',
               orientation = 'h',
               text = xValues,
               textangle = 360,
               textposition = "inside",
               insidetextanchor = "start",
               showlegend = F) %>% 
       layout(
               yaxis = list(zeroline = FALSE,showline = FALSE,showticklabels = FALSE),
               uniformtext = list(mode = "show")
               )

bar
r layout plotly bar-chart r-plotly
1个回答
0
投票

这可以通过如下方式通过add_text添加标签来实现:

BTW:我将向量放在df中。对我来说似乎更自然。

library(plotly)

# Test long text and short bars
xValues <- c("loooooooooooooooooonnnnnngg","lonnnnnnnnnnggggggg",
             "dr3","dr4","dr5")
yValues <- c(0.5,1,2,0.22,10)

df <- data.frame(
  x = xValues,
  y = yValues
)

bar <- plot_ly(df, x = ~y, y = ~x, text = ~x) %>% 
  add_trace(
    type = 'bar',
    orientation = 'h',
    showlegend = F) %>% 
  add_text(x = 0.1, textposition = "middleright") %>% 
  layout(yaxis = list(zeroline = FALSE,showline = FALSE, showticklabels = FALSE))

bar

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.