【问题标题】:Bold plotly (R) pie chart labels粗体 (R) 饼图标签
【发布时间】:2020-09-28 19:50:37
【问题描述】:

我正在努力寻找一种方法来将我的饼图标签加粗。有谁知道如何加粗饼图的标签?这是我的代码:

t <- list(
family = "Ariel",
size = 15,
color = 'black')

fig <-plot_ly()
#####Fall/Winter Concentration
fig <-fig%>% add_pie(concWDper, 
                 labels = concWDper$Compounds, 
                 values = concWDper$Concentration, 
                 type = 'pie',
                 sort = FALSE,
                 textinfo = '', 
                 textfont = list(color = '#000000'),
                 marker = list(colors = c("#9E0142", "#D53E4F", 
                "#F46D43","#FDAE61", "#FEE08B", "#FFFFBF", "#E6F598", 
                "#ABDDA4", "#66C2A5", "#3288BD", "#5E4FA2")),
                 domain = list(x = c(0, 0.5), y = c(0.75, 1)))
#####Spring/Summer Concentration
fig <-fig%>% add_pie(concSPDper, labels = concSPDper$Compounds, 
                 values = concSPDper$Concentration, type = 'pie',sort = FALSE,
                 textinfo = '', textfont = list(color = '#000000'),
                 marker = list(colors = c("#9E0142", "#D53E4F", "#F46D43", 
      "#FDAE61", "#FEE08B", "#FFFFBF", "#E6F598", "#ABDDA4", "#66C2A5", "#3288BD", "#5E4FA2")),
                 domain = list(x = c(0.5, 1), y = c(0.75, 1)))

fig <-fig %>%layout(font=t,showlegend = T,
          grid=list(rows=3, columns=2),
          xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
          yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

fig

这里是一些示例数据:https://docs.google.com/spreadsheets/d/1yarGI5ee5ST_uzeQI-Xa2lk681d24vbIurUl6Rj58YY/edit?usp=sharing

【问题讨论】:

    标签: r label r-plotly


    【解决方案1】:

    粗体标签可以通过texttemplate属性实现,如下所示:

    texttemplate = '&lt;b&gt;%{label}&lt;/br&gt;&lt;/br&gt;%{percent}&lt;/b&gt;'

    使用布局属性margin调整边距,例如

    margin = list(b = 200, l = 100)
    

    使用来自here的示例数据:

    library(plotly)
    USPersonalExpenditure <- data.frame("Categorie"=rownames(USPersonalExpenditure), USPersonalExpenditure)
    data <- USPersonalExpenditure[,c('Categorie', 'X1960')]
    
    p <- plot_ly(data, labels = ~Categorie, values = ~X1960, type = 'pie',sort = FALSE,
                 textinfo = 'label+percent', 
                 texttemplate = '<b>%{label}</br></br>%{percent}</b>', 
                 textfont = list(color = '#000000'),
                 marker = list(colors = c("#9E0142", "#D53E4F", "#F46D43", "#FDAE61", "#FEE08B", 
                                          "#FFFFBF","#E6F598", "#ABDDA4", "#66C2A5", "#3288BD", "#5E4FA2")))
    
    t <- list(
      family = "Times New Roman",
      size = 15,
      color = 'black')
    
    p %>% layout(font=t, margin = list(b = 200, l = 100))
    

    编辑现在使用您的数据

    concWDper <- read.table(text = "Concentration   Compounds
               42   α-pinene
               10   β-pinene
               5    β-phellandrene
               13   camphene
               12   limonene
               3    tricyclene
               2    fenchene
               2    thujene
               7    cymene
               4    sabinene
               1    myrcene", header = TRUE)
    
    t <- list(
      family = "Ariel",
      size = 15,
      color = 'black')
    
    library(plotly)
    
    fig <- plot_ly()
    #####Fall/Winter Concentration
    fig <-fig%>% add_pie(concWDper,
                         labels = concWDper$Compounds, 
                         values = concWDper$Concentration, 
                         type = 'pie',
                         sort = FALSE,
                         textinfo = '', 
                         texttemplate = '<b>%{percent}</b>',
                         textfont = list(color = '#000000'),
                         marker = list(colors = c("#9E0142", "#D53E4F", 
                                                  "#F46D43","#FDAE61", "#FEE08B", "#FFFFBF", "#E6F598", 
                                                  "#ABDDA4", "#66C2A5", "#3288BD", "#5E4FA2")),
                         domain = list(x = c(0, 0.5), y = c(0.75, 1)))
    
    fig <-fig%>% add_pie(data = concWDper, 
                         labels = concWDper$Compounds, 
                         values = concWDper$Concentration, type = 'pie',sort = FALSE,
                         textinfo = '', 
                         texttemplate = '<b>%{percent}</b>',
                         textfont = list(color = '#000000'),
                         marker = list(colors = c("#9E0142", "#D53E4F", "#F46D43", 
                                                  "#FDAE61", "#FEE08B", "#FFFFBF", "#E6F598", "#ABDDA4", "#66C2A5", "#3288BD", "#5E4FA2")),
                         domain = list(x = c(0.5, 1), y = c(0.75, 1)))
    
    fig <-fig %>%layout(font=t,showlegend = T,
                        grid=list(rows=3, columns=2),
                        xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
                        yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
    
    fig
    

    【讨论】:

    • 这适用于 R 吗?文本模板似乎不存在。
    • 当然是 R 的。见https://plotly.com/r/text-and-annotations/#customize-displayed-text-with-a-text-template。代码有什么问题?您使用的是哪个版本的情节?我的是 4.9.2.1。
    • 我有相同的版本。所以它给出了错误:“警告消息:'pie'对象没有这些属性:'texttemplate'有效属性包括:”然后是所有允许的属性
    • 嗯。我没有收到这个警告。此外,我刚刚通过plotly::schema() 查看了饼图属性,其中也列出了texttemplate。但是代码有效吗?毕竟,这只是一个警告。前段时间我有一个类似的问题,r-plotly 抱怨一个属性。但代码仍然有效。
    • 好的,我刚刚更新了所有内容并让它工作了!谢谢!最后一个问题:如何将图例加粗?查看布局属性,我认为它会类似于应用 layoutTemplate = '%{Compounds}',但它不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-11
    • 1970-01-01
    • 2014-12-08
    • 1970-01-01
    相关资源
    最近更新 更多