How to change pie chart segment colors in plotly with r RColorBrewer custom colors

Viewed 213

I'm new to coding, and this is my first time asking a question here so forgive me if this doesn't follow the correct format for questions.
I'm trying to change pie chart colors in plotly with R from the default to RColorBrewer, or, alternatively any custom colors. I've tried all the suggestions I've found on here so far, but none have been successful.
My current code is below which defaults to a blue / orange color combination:

labels = c('Australia','Elsewhere')
values = c(199656, 82883)

fig <- plot_ly(type='pie', labels=labels, values=values,
               textinfo='label+percent',
               insidetextorientation='radial') 

theme(plot.title = element_text(hjust = 0.0))

fig <- fig %>% layout(title = "Logan Population by Birthplace 2016")

fig
1 Answers

You could set custom colors via the colors attribute of marker. For more on styling pie charts see here:

library(plotly)

labels <- c("Australia", "Elsewhere")
values <- c(199656, 82883)

cols <- RColorBrewer::brewer.pal(3, "Blues")[seq_along(labels)]

plot_ly(
  type = "pie",
  labels = labels,
  values = values,
  marker = list(colors = cols),
  textinfo = "label+percent",
  insidetextorientation = "radial"
) %>%
  layout(title = "Logan Population by Birthplace 2016")

Related