Bold plotly (R) pie chart labels

2.2k views Asked by At

I am struggling to find a method that will bold my pie chart labels in plotly. Does anyone know how to bold the labels of a plotly pie chart? Here is my code:

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

Here is some example data: https://docs.google.com/spreadsheets/d/1yarGI5ee5ST_uzeQI-Xa2lk681d24vbIurUl6Rj58YY/edit?usp=sharing

1

There are 1 answers

12
stefan On BEST ANSWER

Bold labels could be achieved via the attribute texttemplate like so:

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

To adjust the margins make use of layout attribute margin, e.g.

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

Using the example data from 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))

enter image description here

EDIT Now with your data

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

enter image description here