gpt4 book ai didi

r - 如何绘制用一种缩放颜色着色的饼图并使用 plotly 包

转载 作者:行者123 更新时间:2023-12-04 09:15:08 24 4
gpt4 key购买 nike

我有这个示例数据框:

> 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))

它给了我这个饼图:

enter image description here

但我不需要获得很多颜色,我只需要一种缩放颜色,其强度随着变量 Pourcentages 的变化而增加。

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

enter image description here

感谢您的帮助!

最佳答案

这是扩展 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

关于r - 如何绘制用一种缩放颜色着色的饼图并使用 plotly 包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51666355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com