R 如何使用plotly软件包以一种缩放颜色绘制饼图

R 如何使用plotly软件包以一种缩放颜色绘制饼图,r,R,我有一个示例数据框: > 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

我有一个示例数据框:

> 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
然后,我想只使用
plotly
包绘制一个饼图

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))
库(dplyr)
图书馆(绘本)
p%
布局(标题=‘1960年按类别划分的美国个人支出’,
xaxis=list(showgrid=FALSE,zeroline=FALSE,showticklabels=FALSE),
yaxis=list(showgrid=FALSE,zeroline=FALSE,showticklabels=FALSE))
它给了我这个饼图:

但我不需要得到很多颜色,我只需要一个缩放颜色,它的强度随着变量
Pourcentages
的增加而增加

换句话说,我想要一个饼图,使用
plotly
包,如下所示:

谢谢你的帮助

演示数据:
# 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

数据下面是另一个解决方案,从以下方面扩展了答案:

数据更简单的解决方案:

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)
库(dplyr)
图书馆(绘本)
##

是这样的吗@RonakShah,不,它是不一样的,因为在那个例子中,它有很多颜色(红色,绿色…)。我需要一个color@RonakShah,不,这不是因为我只需要使用颜色,但在您的示例中有许多颜色(红色、绿色…),您很少会遇到与您的问题完全相同的问题。您需要根据您的需要调整类似的答案,如@SYMBOLRUSHANOTO Roldan Diaz的答案所示,谢谢,但它不是相同的数据框,因此不会给出需要的结果。我邀请您重新放置您的dataframe
数据这是一个示例,您可以更改颜色和百分比,它继续工作,我尝试过使用您的数据,它看起来不错,但是我的示例的百分比从0到1,您的从0到100,这就是为什么我在选择颜色时乘以100,您应该修改该部分(
colorrmppalete(brewer.pal(9,“Blues”)(100)[数据$Pourcentages]
)。谢谢你,伙计!对于条形图,有类似的解决方案吗?
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)