2

Do we have something similar to our ggplot where we define the color panel for each variable ?

scale_manual <- function(...){
ggplot2::manual_scale(
"fill",
values = setnames(c("green","red","blue","yellow","grey"),
c("var1","var2","var3","var4","var5")),
...
)
}

Although this Q seems to answer, How can I change the colors of the slices in pie charts in plotly for r using hexadecimal strings?

but it is not working here.

Please consider an reprex below:

library(plotly)

USPersonalExpenditure <- data.frame("Categorie"=rownames(USPersonalExpenditure), USPersonalExpenditure)
data <- USPersonalExpenditure[,c('Categorie', 'X1960')]

p <- plot_ly(data, labels = ~Categorie, values = ~X1960, type = 'pie') %>%
  layout(title = 'United States Personal Expenditures by Categories in 1960',
         xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

#trial 1
plot_ly(data, labels = ~Categorie, values = ~X1960, type = 'pie', marker = list(color = rainbow(5))) %>%
layout(title = 'United States Personal Expenditures by Categories in 1960', 
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

# trial 2
plot_ly(data, labels = ~Categorie, values = ~X1960, type = 'pie', marker = list(color = brewer_pal(5, "Set3"))) %>%
layout(title = 'United States Personal Expenditures by Categories in 1960', 
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

Since there are many plots using the same data, color need to be consistent.

So, trying to hard code for each variable.

Abhishek
  • 407
  • 3
  • 18

1 Answers1

4

I usually use Color mapping functions from the leaflet package before making interactive plots for shiny apps and plotly charts:

Hard code the color variable using your desired palette.

data$color <- leaflet::colorFactor(
  palette = "Dark2", domain = data$Categorie
  )(data$Categorie)

plot_ly(
  data, labels = ~Categorie,  values = ~X1960, type = 'pie',
  marker = list( colors = ~color)
  ) %>%
  layout(
    title = 'United States Personal Expenditures by Categories in 1960', 
    xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
    yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE)
    )

the pie chart

You may also hardcode it manually:

colors_list <- list(
  "Food and Tobacco" = "#1B9E77",
  "Household Operation" = "#D95F02",
  "Medical and Health" = "#7570B3",
  "Personal Care" = "#E7298A",
  "Private Education" = "#66A61E"
)

data$color <- dplyr::recode(data$Categorie, !!!colors_list)


plot_ly(
  data, labels = ~Categorie,  values = ~X1960, type = 'pie',
  marker = list( colors = ~color)
) %>%
  layout(
    title = 'United States Personal Expenditures by Categories in 1960', 
    xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
    yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE)
  )
maaniB
  • 595
  • 6
  • 23
  • Thanks for adding solution. currently, unable if install leaflet tried both install.packages("leaflet") and (!require('devtools')) install.packages('devtools') devtools::install_github('rstudio/leaflet') Issue with "png" package itself – Abhishek Aug 22 '19 at 11:33
  • @Abhi leaflet is a very useful and popular package. You can install its dependencies manually. What is your `Sys.info()`? Also, I'm gonna add another method for hardcoding the colors by editing my response. – maaniB Aug 22 '19 at 11:39
  • Error starts with PNG. read.c:3:17: fatal error: png.h: No such file or directory #include ^ compilation terminated. make: *** [read.o] Error 1 ERROR: compilation failed for package ‘png’ * removing ‘/home/myUserID/R/x86_64-redhat-linux-gnu-library/3.4/png’ Warning in install.packages : installation of package ‘png’ had non-zero exit status ERROR: dependency ‘png’ is not available for package ‘leaflet’ * removing ‘/home/myuserID/R/x86_64-redhat-linux-gnu-library/3.4/leaflet’ Warning in install.packages : installation of package ‘leaflet’ had non-zer – Abhishek Aug 22 '19 at 11:44
  • close your rstudio and do not save the workspace image. remove png package in your rstudio (`remove.packages("png", lib="~/home/myUserID/R/x86_64-redhat-linux-gnu-library/3.4")`) and delete it from your `/home/myUserID/R/x86_64-redhat-linux-gnu-library/3.4/png`. Then, `install.packages("png")`. If it is successful, you can install `leaflet` – maaniB Aug 22 '19 at 11:49
  • thanks for helping me.... It was an issue with png.... https://stackoverflow.com/questions/21800909/cannot-build-r-package-png-fedora-20 – Abhishek Aug 22 '19 at 11:50
  • If all the problems are solved and you think my solution is enough you can accept it :-) Good luck. – maaniB Aug 22 '19 at 11:52
  • How can we disable the legend ? describing the color ? – Abhishek Aug 26 '19 at 10:21