如何使用十六进制字符串在曲线图中更改饼图中切片的颜色?

时间:2016-06-25 03:43:49

标签: r pie-chart plotly

这是我现在的饼图:

library(plotly)
library(RColorBrewer)


P <- data.frame (labels = c("A", "B", "C", "D", "E"),
             values = c(5, 8, 3, 4, 9))

plot_ly(P, labels = labels, values = values, type = "pie",
    marker = list(colors=c("lightskyblue", "deepblue", "dodgerblue", "midnightblue", "powderblue")),
    textinfo="value",
    textposition="outside") 

我想用十六进制字符串改变颜色,所以我可以使用来自RColorBrewer的调色板。提前谢谢!

2 个答案:

答案 0 :(得分:4)

只需将十六进制值放入字符串中,然后使用哈希(井号)plot_ly(P, labels = labels, values = values, type = "pie", marker = list(colors=c("#556677", "#AA3344", "#772200", "#11AA22", "#AA231B88")), # the last color has alpha value set. textinfo="value", textposition="outside") 。十六进制的前两位数字表示红色,下一页表示绿色,2位数表示蓝色(#RRGGBB)。您可以选择为alpha(透明度)添加两位数(#RRGGBBAA)。

e.g

modBaseAddr

答案 1 :(得分:3)

探索RColorBrewer

   library(RColorBrewer)

查看RColorBrewer包中的函数列表

   ls("package:RColorBrewer")
   #  [1] "brewer.pal"         "brewer.pal.info"    "display.brewer.all"
   #  [4] "display.brewer.pal"

显示所有配色方案

   display.brewer.all()

获取Blues十六进制字符串

   brewer.pal(9,"Blues")
   # [1] "#F7FBFF" "#DEEBF7" "#C6DBEF" "#9ECAE1" "#6BAED6" "#4292C6" "#2171B5"
   # [8] "#08519C" "#08306B"

   brewer.pal(10,"Blues")
   # [1] "#F7FBFF" "#DEEBF7" "#C6DBEF" "#9ECAE1" "#6BAED6" "#4292C6" "#2171B5"
   # [8] "#08519C" "#08306B"
   # Warning message:
   # In brewer.pal(10, "Blues") :
   # n too large, allowed maximum for palette Blues is 9
   # Returning the palette you asked for with that many colors

观看布鲁斯舞团

  display.brewer.pal(9,"Blues")

您可以获得的颜色数量有限制,但如果您想扩展Sequential或Diverging组,可以使用colorRampPalatte命令进行扩展,例如:

  colorRampPalette(brewer.pal(9,”Blues”))(100)

divergentqualitativesequential计划的示例。 SpectralSet2Reds,可以使用上面提到的命令display.brewer.all()查看这些名称。您可以使用列表中的其他一些方案。

 display.brewer.pal(4,"Spectral")
 brewer.pal(4,"Spectral")
 # [1] "#D7191C" "#FDAE61" "#ABDDA4" "#2B83BA"

 display.brewer.pal(4,"Set2")
 brewer.pal(4,"Set2")
 # [1] "#66C2A5" "#FC8D62" "#8DA0CB" "#E78AC3"

 display.brewer.pal(4,"Reds")
 brewer.pal(4,"Reds")
 # [1] "#FEE5D9" "#FCAE91" "#FB6A4A" "#CB181D"