【问题标题】:How do I allow Plotly bar text to overflow past bar?如何允许 Plotly 栏文本溢出过去栏?
【发布时间】:2020-07-31 11:35:05
【问题描述】:

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

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

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】:

    这可以通过add_text 添加标签来实现,如下所示:

    顺便说一句:我将向量放在 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
    

    【讨论】:

    • 理想情况下,我想避免对任何坐标进行硬编码。我要删除的文本前面还有一个空格。
    • 对不起。您可以通过设置x = 0 删除标签前的空格。我也不喜欢硬编码。但是,在这种情况下,我认为将 x 设置为 0 是可以接受的,因为条形图自然开始(或应该从零开始 (;)。
    猜你喜欢
    • 1970-01-01
    • 2018-09-11
    • 1970-01-01
    • 2023-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    • 2014-05-13
    相关资源
    最近更新 更多