是否可以覆盖R绘图条形图中标签的字体大小限制?

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

我有一个带有标签的plotly分组条形图。我可以更改标签上的字体大小,但是plotly不允许字体超过某个特定限制,该限制似乎由条的宽度/高度确定。当要绘制大量的组和/或值时,这将成为问题。在某些情况下,文本不会变得比例如8号字体大。有没有超出此限制的方法?

这里有一些示例代码:

library(dplyr)
library(plotly)
data.frame(Desc = c("A", "A", "B", "B", "C", "C"), 
           Group = c("Group1", "Group2", "Group1", "Group2", "Group1", "Group2"), 
           Value = c(5, 4, 6, 4, 5, 3)) %>% 
  plotly::plot_ly(data = .) %>% 
  plotly::add_trace(x = ~Value, 
                    y = ~Desc, 
                    color = ~Group,
                    type = 'bar', 
                    text = ~Value, 
                    textposition = "outside", 
                    textfont = list(size = 10), # size is defined here
                    )

此图表的字体大小为10,输出:enter image description here

如果我将字体更改为30磅,则:enter image description here

最后,在50点(相对于30点不变):enter image description here

r plotly
1个回答
0
投票

您可以使用layout并设置uniformtext来更改文字大小。我增加了x轴范围以使大数适合。

suppressPackageStartupMessages({
library(dplyr);
library(plotly)})
data.frame(Desc = c("A", "A", "B", "B", "C", "C"), 
           Group = c("Group1", "Group2", "Group1", "Group2", "Group1", "Group2"), 
           Value = c(5, 4, 6, 4, 5, 3)) %>% 
    plotly::plot_ly(data = .) %>% 
    plotly::add_trace(x = ~Value, 
                      y = ~Desc, 
                      color = ~Group,
                      type = 'bar', 
                      text = ~Value, 
                      textposition = "outside"
    ) %>% layout(xaxis=list(range=c(0, max(.$Value)+2)),
                 uniformtext=list(minsize=36, mode='show'))

“”

reprex package(v0.3.0)在2020-05-29创建

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