【问题标题】:Changing the fontsize of annotation in R plotly更改R中注释的字体大小
【发布时间】:2020-09-14 11:27:26
【问题描述】:

我想增加 plotly 图形上文本注释的大小。我想出的解决方案是在下面的最小代码上。但是,一旦位数通过该值增加,则文本的字体大小不会以相同的方式应用于所有位数成员。为了向大家展示实际问题,我准备了一个伪输入数据,所以请不要专注于为什么要显示一些愚蠢的数字。这个问题看起来很小,但它严重影响了我波动的数据可视化,这些可视化在序列中具有各种深度和最高值。有什么想法吗?

最少的代码

library(plotly)

df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv", stringsAsFactors = FALSE)
df <- df[which(df$year==2007 & df$continent=='Europe' & df$pop > 2.e6),]

#pseudo input data
temp_data <- c(c(1:5, 10:15, 100:105, 1000:1010))

fig <- plot_ly(df, type='bar', x = ~country, y = temp_data, text = temp_data, name="",
               textposition = 'outside', #'auto' 
               textangle    = -90,
               textfont     = list(size = 120)
               )

fig

电流输出

期望是这样的

【问题讨论】:

    标签: r graphics annotations font-size r-plotly


    【解决方案1】:

    您可以使用layout 来做到这一点。为了适应数字,我还增加了 y 轴范围。

    library(plotly)
    #> Loading required package: ggplot2
    #> 
    #> Attaching package: 'plotly'
    #> The following object is masked from 'package:ggplot2':
    #> 
    #>     last_plot
    #> The following object is masked from 'package:stats':
    #> 
    #>     filter
    #> The following object is masked from 'package:graphics':
    #> 
    #>     layout
    
    df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv", stringsAsFactors = FALSE)
    df <- df[which(df$year==2007 & df$continent=='Europe' & df$pop > 2.e6),]
    
    #pseudo input data
    temp_data <- c(c(1:5, 10:15, 100:105, 1000:1010))
    
    fig <- plot_ly(df, type='bar', x = ~country, y = temp_data, text = temp_data, name="",
                   textposition = 'outside', #'auto'
                   textangle    = -90) %>% layout(yaxis=list(range=c(0, max(temp_data)+200)),
                                                  uniformtext=list(minsize=18, mode='show'))
    fig
    

    reprex package (v0.3.0) 于 2020 年 5 月 27 日创建

    【讨论】:

    • 这绝对是完美的答案!这正是我想看到的。
    • 很高兴它对您有用。也许您仍然可以沿同一方向旋转注释和 X 轴标签,但...
    • 我发现您的建议适应了我的代码,没有任何问题。所以整个旋转和标签都很好。但是,我不确定我是否真的理解您在布局部分的计划和编码:(例如 +200 的目标?uniformtext 的目的是关键?
    • 如果省略 200,则 y 轴变为 1000,并且标签被切断。所以增加 Y 范围会给你更多的空间。 uniformtext 允许你调整字体大小(见:plotly.com/r/text-and-annotations/…
    猜你喜欢
    • 1970-01-01
    • 2019-01-02
    • 2016-12-26
    • 2017-10-21
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 2021-10-09
    • 2022-08-24
    相关资源
    最近更新 更多