如何使用十六进制字符串更改R中饼图中的切片颜色? [英] How can I change the colors of the slices in pie charts in plotly for r using hexadecimal strings?

查看:115
本文介绍了如何使用十六进制字符串更改R中饼图中的切片颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我目前的饼图:

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中的调色板.预先谢谢你!

I wanted to change its colors with hexidecimal strings so I could use palettes from RColorBrewer. Thank you in advance!

推荐答案

只需将十六进制值放入字符串中,并在其前面加上一个哈希(磅符号)#.十六进制的前两位数字表示红色,后两位数字表示绿色,然后两位数字表示蓝色(#RRGGBB). (可选)您可以为透明度(#RRGGBBAA)添加额外的两位数字.

Just put the hex values into the strings, preceded with a hash (pound symbol) #. the first two digits in hex are for red, the next 2 for green, then 2 digits for blue (#RRGGBB). Optionally you can add an extra two digits for the alpha (transparency) (#RRGGBBAA).

例如

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

这篇关于如何使用十六进制字符串更改R中饼图中的切片颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆