'Highcharter: Legend not appearing with the "color" argument in hchart()

I've produced a plot using highcharter, where my x-axis is a discrete product variable, my y-axis is the total price of the units sold, and I coloured the bars with the number of units sold. By default, there's no legend. I'm also struggling to customize the colour palette (uses viridis as default).

Here's sample data:

library(tidyverse)

sample_df <- tibble(product = c("Product A", "Product B", "Product C","Product D"),
       total_sales = c(15000,12000,9000,18000),
       units_sold = c(62,24,35,24)
)



# A tibble: 4 x 3
  product   total_sales units_sold
  <chr>           <dbl>      <dbl>
1 Product A       15000         62
2 Product B       12000         24
3 Product C        9000         35
4 Product D       18000         24

What I've done so far in highcharter:

library(highcharter)
library(viridis)

sample_df %>%
  hchart('column', hcaes(x = product, y = total_sales, color = units_sold))

enter image description here

An example of what I'd like to have (using ggplot):

sample_df %>%
  ggplot(aes(x = product, y = total_sales, fill = units_sold)) +
  geom_bar(stat = "identity") +
  scale_fill_viridis_b()

enter image description here

Ideally, I'd also like to be able to choose the colour palette, such as:

scale_fill_viridis_b(option = "magma")

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source