The short answer is to set the markers.
Your original set up was confusing to me, as it seemed you were trying to apply 4 different sets of colouring to one chart. I only made sense of this by making 4 charts, one for each set of colours.
library(plotly)
library(RColorBrewer)
library(tidyverse)
sector1 <- tibble::tribble(
~Subsector, ~Greenhouse_gas,
"Energy", 73.2,
"Agriculture, Forestry and Land Use", 18.4,
"Industry", 5.2,
"Waste", 3.2
)
brewer_seq <- c("BuGn", "GnBu", "BuPu", "Blues")
(brewer_cols_set <- map(
brewer_seq,
~ brewer.pal(
n = length(unique(sector1$Subsector)),
name = .
)
) %>% set_names(brewer_seq))
myfunc <- function(x,y) {
cols <- unlist(x)
title <- y
plot_ly(
data = sector1,
labels = ~Subsector,
values = ~Greenhouse_gas,
type = "pie", sort = FALSE,
marker = list(colors = cols)
) %>% layout(title = title)
}
plot_list <- imap(.x = brewer_cols_set, .f = myfunc)
plot_list[[1]]
plot_list[[2]]
plot_list[[3]]
plot_list[[4]]