rplotechartsecharts4rviridis

echarts4r error at plotting barchart's colors


As you can see in the annexed image it seems that there is a problem with the e_visual_map() function from echarts4r. After y = aa the colors stop changing. Is there a way to fix the bar's colors?

data

library(dplyr)
library(viridis)
library(echarts4r)

df <- structure(
  list(
    Market = c("a", "b", "c", "d", "e", 
              "f", "g", "h", "i", "j", 
              "k", "l", "m", "n", "o",
              "p", "q", "r", "s", "t", 
              "u", "v", "w", "x", "y", 
              "z", "aa", "bb", "cc", 
              "dd", "ee", "ff", "gg", 
              "hh", "ii", "jj", "kk", 
              "ll", "mm", "nn", "oo", 
              "pp", "qq", "rr", "ss", 
              "tt", "uu", "vv"), 
    Percent_Change = c(5.16901350940851, 3.91868856906443, 3.41802504497987, 3.16413673886071, 
                       3.12684219659363, 2.89249688621206, 2.87284606849977, 2.84454222482254, 
                       2.57058275282915, 2.43282934768581, 2.34818492965906, 2.30880001810456, 
                       2.2918613260413,  2.24101659933832, 2.18752627680741, 2.10073586714032, 
                       1.86045092759311, 1.85290305266011, 1.68128474330245, 1.54700002004653, 
                       1.5303536712395,  1.52152376952798, 1.45917880532612, 1.4355692973819, 
                       1.4257368870892,  1.36409659669896, 1.22315092771929, 1.04309133074753, 
                       0.939025651002292, 0.844389462321624, 0.797407599768931, 0.681691408815433, 
                       0.242176237950194, 0.237798995363376, 0.219182593926239, -0.0280421490193321, 
                       -0.111286439923117, -0.124395342178022, -0.175922623382462, -0.188080671185304, 
                       -0.870155958402443, -1.60611679230328, -1.66206110148814, -1.82732601610943, 
                       -3.68051100830324, -4.43292411223474, -9.42691532047856, -10.5405968097707)), 
  row.names = c(NA, -48L), class = c("tbl_df", "tbl", "data.frame"))

code to plot

df %>% arrange(Percent_Change) %>% 
  # mutate(Market = fct_reorder(Market, -Percent_Change)) %>% 
  e_chart(Market) %>% e_bar(Percent_Change) %>% 
  e_visual_map(Percent_Change, scale = e_scale, color = viridis(100)) %>% 
  e_flip_coords() %>% 
  e_legend(show = F) %>% 
  e_color(background = c("#343E48"))

Results enter image description here


Solution

  • Not 100% about your desired result. If you want to color your bars according to the value of PercentChange than this could be achieved by setting dimension=0 to color by the x axis and getting rid of scale:

    library(dplyr)
    library(viridis)
    library(echarts4r)
    
    df %>%
      arrange(Percent_Change) %>%
      e_chart(Market) %>%
      e_bar(Percent_Change) %>%
      e_visual_map(Percent_Change,
        color = viridis(10),
        dimension = 0
      ) %>%
      e_flip_coords() %>%
      e_legend(show = FALSE) %>%
      e_color(background = c("#343E48"))
    

    enter image description here