2
votes

I have this sample data frame :

> Data
                    Produits Pourcentages
1              Crème de jour        27.10
2                      sérum        14.50
3              Crème de nuit        13.80
4                     masque         8.82
5      démaquillant à rincer         7.73
6  démaquillant sans rincage         7.24
7                     lotion         6.57
8                eau florale         5.83
9                      huile         5.65
10          produits teintés         2.82

Then, I want to plot a pie chart using only the plotly package.

library(dplyr)
library(plotly)

p <- plot_ly(Data, labels = ~Produits, values = ~Pourcentages, 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))

It gives me this pie chart:

enter image description here

But I don't need to get many colors, I just need one scaled color which has an intensity that increases in function of the variable Pourcentages.

In another word, I want a pie chart using plotly package like this:

enter image description here

Thank you for your help !

3
@RonakShah, no it si not the same because in that example it has many colors (red,green...). I need one colorRprogrammer
@RonakShah, no it is not because I need to use only color, but in your example there is many color (red,green...)Rprogrammer
There are very rare chances that you will get question exactly same as yours. You need to adapt the similar answers which are available according to your need as shown in the answer by @symbolrushRonak Shah

3 Answers

6
votes

Here's another solution expanding the answer from here:

Data <- data.frame(
  Produits = c("Crème de jour", "sérum", "Crème de nuit", "masque", "démaquillant à rincer", "démaquillant sans rincage", "lotion", "eau florale", "huile", "produits teintés"),
  Pourcentages = c(27.10, 14.50, 13.80, 8.82, 7.73, 7.24, 6.57, 5.83, 5.65, 2.82)
)
gradient <- colorRampPalette(c('lightblue', 'darkblue'))
Data$colors <- gradient(dim(Data)[1])[as.numeric(cut(Data$Pourcentages, breaks = dim(Data)[1]))]
plot_ly(Data, labels = ~Produits, values = ~Pourcentages, type = 'pie', marker = list(colors = ~colors))

enter image description here

1
votes
# Demo data:
Data <- data.frame(Produits = letters[1:5], Pourcentages = (1:5)/15)

# List of colors:
library(RColorBrewer)
colors = colorRampPalette(brewer.pal(9, "Blues"))(100)[100*Data$Pourcentages]

# Chart:
p <- plot_ly(Data, labels = ~Produits, values = ~Pourcentages, type = 'pie',
             marker = list(colors = colors)) %>%
  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),
         showlegend = TRUE)
p

enter image description here

1
votes

more simple solution :

library(dplyr)
library(plotly)

##
Produits<-c("Crème de jour","sérum","Crème de nuit","masque","démaquillant à rincer",
            "démaquillant sans rincage","lotion","eau florale","huile","produits teintés")
Pourcentages<-c(27.1,14.5,13.8,8.82,7.73,7.24,6.57,5.83,5.65,2.82)
colors<-c("#ff0000","#ff1919","#ff3232","#ff4c4c","#ff6666",
          "#ff7f7f","#ff9999","#ffb2b2","#ffcccc","#ffe5e5")

Data<-data.frame(Produits,Pourcentages,colors)


plot_ly(Data, labels = ~Produits, values = ~Pourcentages, type = 'pie', marker = list(colors = ~colors))%>%
  layout(title = 'Les pourcentages des types de soins préférés',
         xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
         showlegend = TRUE)

enter image description here