Home > Mobile >  What is the way to tell highcharter which colors to use for a pie chart using R?
What is the way to tell highcharter which colors to use for a pie chart using R?

Time:01-17

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

pie chart without color attribute

With the color-attribute I get

pie chart with color attribute

You can recognize the viridis colors

first three viridis colors

but it is definitely not 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

pie chart with changed color

The function hc_add_series_labels_values seems to be deprecated in my version: 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?

CodePudding user response:

To explicitly inform colors, you could try the following.

In your data.frame, including y for the count, and keep the same name and color. Then, provide this data to 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

highcharter pie chart with defined colors

  • Related