dreamRs / apexcharter

:bar_chart: R Htmlwidget for ApexCharts.js
https://dreamrs.github.io/apexcharter
Other
140 stars 15 forks source link

Provide option to keep colors fixed for each factor level (when subsetting) #25

Closed henningsway closed 2 years ago

henningsway commented 4 years ago

I plan to use your package in a shiny app, where I need to visualize the effects of a variable with many factor levels (20-30). In this context it's important, that the colors for the specific factor levels don't change when some kind of filter or drill down happens.

In ggplot2 this can be achieved by setting scale_color_discrete(drop = FALSE). Is there a similar solution that can be used or implemented in the apexcharter package (potentially ax_colors(drop = FALSE) or so)?

By the way, the package has been a deligth to use so far! :)

library(tidyverse)
library(apexcharter)

mtcars_fct <- mtcars %>% 
  modify_at("cyl", factor)

mtcars_fct %>% 
  filter(cyl != "4") %>% 
  ggplot(aes(wt, mpg, color = cyl)) +
  geom_point() +
  scale_color_discrete(drop = FALSE)


mtcars %>% 
  filter(cyl != "4") %>% 
  apex(type = "scatter",
       mapping = aes(x = wt, y = mpg, fill = cyl))

Created on 2020-07-27 by the reprex package (v0.3.0)

henningsway commented 4 years ago

PS: for some strange reason I couldn't create an apex-chart with the penguins dataset.

# library(palmerpenguins)
# apex(data = penguins,
#      type = "scatter",
#      mapping = aes(x = bill_length_mm, y = bill_depth_mm))
malouche commented 4 years ago

Can you please help to customize the colors of the lines in this code:

apex(data = dt, type = "line", mapping = aes(x = date, y = value,color=variable)) %>% ax_stroke(width = 2,curve = "smooth")%>% ax_yaxis(title = list(text = ""), labels = list( formatter = htmlwidgets::JS(FUNC_JSFormatNumber) ))%>% ax_grid(borderColor = "#e7e7e7", row = list( colors = c("#f3f3f3", "transparent") ))%>% ax_title( text = "Cumulative COVID-19 Cases", align = "left" )%>% ax_legend(position="top")

pvictor commented 4 years ago

@henningsway yes the color management is not great at the moment, they are not actually linked to the data on the JavaScript side. I think we can do something in R to fix that.

The issue with penguins dataset is due to missing value, that's fixed.

@malouche Can you open a new issue with a reproducible example please ? I'll look into it

pvictor commented 2 years ago

Check ax_colors_manual() for that functionnality.