【问题标题】:How plot a pie chart colored with one scaled color and using plotly package如何绘制用一种缩放颜色着色并使用 plotly 包的饼图
【发布时间】:2018-08-03 06:17:12
【问题描述】:

我有这个示例数据框:

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

它给了我这个饼图:

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

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

感谢您的帮助!

【问题讨论】:

  • @RonakShah,不,它不一样,因为在那个例子中它有很多颜色(红色,绿色......)。我需要一种颜色
  • @RonakShah,不,这不是因为我只需要使用颜色,而是在你的示例中有很多颜色(红色,绿色......)
  • 很少有机会遇到与您的问题完全相同的问题。您需要根据您的需要调整可用的类似答案,如@symbolrush 的答案所示

标签: r


【解决方案1】:

这是从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))

【讨论】:

    【解决方案2】:
    # 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
    

    【讨论】:

    • Antonio Roldan Diaz,谢谢,但它不是同一个数据框,所以它没有给出需要的结果。我邀请您将您的数据框 Data &lt;- data.frame(Produits = letters[1:5], Pourcentages = (1:5)/15) 替换为 Data&lt;-data.frame(Produits,Pourcentages),您会看到差异
    • 这是一个示例,您可以更改颜色和百分比,它会继续工作,我已经尝试使用您的数据,它看起来不错,但是我示例的百分比从 0 到 1,而您的百分比从 0到 100,这就是为什么我在选择颜色时乘以 100,你应该修改那部分 (colorRampPalette (brewer.pal (9," Blues ")) (100) [Data $ Pourcentages])。
    【解决方案3】:

    更简单的解决方案:

    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)
    

    【讨论】:

    • 谢谢队友!对于条形图有类似的解决方案吗?
    猜你喜欢
    • 1970-01-01
    • 2020-12-22
    • 1970-01-01
    • 2022-01-10
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    • 1970-01-01
    相关资源
    最近更新 更多