简体   繁体   English

使用R的饼图告诉highcharter使用哪个colors的方法是什么?

[英]What is the way to tell highcharter which colors to use for a pie chart using R?

I want to create a pie chart using this code:我想使用这段代码创建一个饼图:

require(highcharter)
hdf=data.frame(count=c(11,15,25),name=c('a','b','c'),color=c('green','red','yellow'))
hc=hchart(hdf,
          'pie',
          hcaes(x=name,y=count,color=color),
          name='Name')
hc

However, stangely the outcome is not what one would expect.然而,奇怪的是,结果并不是人们所期望的。

Without the color-attribute in hcaes I get没有hcaes中的颜色属性我得到

没有颜色属性的饼图

With the color-attribute I get使用颜色属性我得到

带颜色属性的饼图

You can recognize the viridis colors你可以认出viridis colors

前三种绿色

but it is definitely not c('green','red','yellow') .但绝对不是c('green','red','yellow') However, the colors c('green','red','yellow') actually do change something: If you use color=c('red','green','yellow') instead, you get然而, colors c('green','red','yellow')实际上改变了一些东西:如果你使用color=c('red','green','yellow')代替,你得到

颜色改变的饼图

The function hc_add_series_labels_values seems to be deprecated in my version: highcharter_0.9.4 . function hc_add_series_labels_values在我的版本中似乎已弃用: highcharter_0.9.4 What is now the way to go to achieve what I want, using a color column in my data.frame for custom colors of my plot?现在 go 实现我想要的方法是什么,在我的data.frame中使用颜色列来自定义 plot 的 colors?

To explicitly inform colors, you could try the following.要明确通知 colors,您可以尝试以下操作。

In your data.frame, including y for the count, and keep the same name and color .在您的 data.frame 中,包括y用于计数,并保持相同的namecolor Then, provide this data to hc_add_series .然后,将此数据提供给hc_add_series

library(highcharter)

hdf = data.frame(
  y = c(11, 15, 25),
  name = c('a', 'b', 'c'),
  color = c('green', 'red', 'yellow')
)

highchart() |> 
  hc_chart(type = "pie") |> 
  hc_add_series(data = hdf)

Plot Plot

具有定义颜色的 highcharter 饼图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM