JohnCoene / echarts4r

🐳 ECharts 5 for R
http://echarts4r.john-coene.com/
Other
595 stars 81 forks source link

mirror plot #582

Closed antoine4ucsd closed 11 months ago

antoine4ucsd commented 11 months ago

Hello Congrats for this amazing set of tools! I have a quick question that I was not able to resolve. I created this mirror plot but I would like the label and tooltip to show the absolute value (i.e. no negative) is it possible? or is there an alternative approach for mirror plots?

thank you!

image

rdatasculptor commented 11 months ago

Can you give some example code to play with?

antoine4ucsd commented 11 months ago

sure. I came up with this:

df_wide|>
        e_chart(age_group)|>
        e_bar(Female,stack = "grp")|>
        e_bar(Male,stack = "grp")|>
                  e_flip_coords()

using the attached( can be transformed into long format with grouping I guess) thank you!

df_wide.csv df.csv

rdatasculptor commented 11 months ago

enjoy!

df_wide <- data.frame(
  stringsAsFactors = FALSE,
         age_group = c("0-9","10-19","18-29",
                       "30-39","40-49","50-59","60-69","70-79","80+"),
            Female = c(1L, 4L, 40L, 25L, 9L, 3L, 1L, 1L, 0L),
              Male = c(-1L, -13L, -851L, -1005L, -361L, -62L, -8L, -2L, 0L)
)

df_wide|>
  e_chart(age_group) |>
  e_bar(Female,stack = "grp") |>
  e_bar(Male,stack = "grp") |> 
  e_y_axis(axisLabel = list(show = TRUE, formatter = htmlwidgets::JS(
    'function(value){return Math.abs(value);}'))) |>
  e_flip_coords() 

The trick is to use some javaScript in the formatter argument of the axis label.

antoine4ucsd commented 11 months ago

wonderful! I still have a lot to learn but this is inspiring. thank you!