Rprogrammer
Rprogrammer

Reputation: 477

How plot a pie chart colored with one scaled color and using plotly package

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 !

Upvotes: 2

Views: 3301

Answers (3)

Rprogrammer
Rprogrammer

Reputation: 477

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

Upvotes: 1

symbolrush
symbolrush

Reputation: 7457

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

Upvotes: 6

# 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

Upvotes: 1

Related Questions