Reputation: 477
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:
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:
Thank you for your help !
Upvotes: 2
Views: 3301
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)
Upvotes: 1
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))
Upvotes: 6
Reputation: 2047
# 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
Upvotes: 1