Change Color of Pie Chart

Hi everyone,

For the life of me I cant seem to figure out how to change the colors of my pie chart. The colors keep displaying the default.

Data :

tibble::tribble(
~Subsector, ~Greenhouse_gas,
"Energy", 73.2,
"Agriculture, Forestry and Land Use", 18.4,
"Industry", 5.2,
"Waste", 3.2
)

Code:

brewer_seq <- c( "BuGn", "GnBu", "BuPu", "Blues")

pie <- plot_ly(data = sector1,
labels= ~Subsector,
values= ~Greenhouse_gas,
type = 'pie', sort = FALSE,
colors=brewer_seq)%>%
layout(title = "Greenhouse gases by Industry")

Thank you in advance!!

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]]
1 Like

Thank you for this!

However when i save it onto my blogdown website - the color codes show up even though my rchuck is set on echo=false, message=false and warning = false

Would you happen to know how I can get rid of that code out of the outputs?

image

thank you again for your help

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.