繁体   English   中英

粗体 (R) 饼图标签

[英]Bold plotly (R) pie chart labels

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

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

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

texttemplate = '<b>%{label}</br></br>%{percent}</b>'

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

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

使用这里的示例数据:

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

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM