Essossinam
Essossinam

Reputation: 1

How to get the axis label on the bars in R using echart4r package?

This is what I want

1

I am trying to replicate the chart above (with the labels on the bars) in R using echart4r package. For the axis labels to appear on the bars, I used axisLabel = list(inside = TRUE) inside e_x_axis() as in the code below. But this does not work as the labels are hidden by the bars.

Name <- c("Jon", "Bill", "Maria", "Ben", "Tina")
Age <- c(23, 41, 32, 58, 26)

df <- data.frame(Name, Age)

df %>% 
  e_charts(Name) %>% 
   e_bar(Age) %>% 
    e_x_axis(
      inverse = TRUE,
      axisLabel = list(inside = TRUE),
      axisTick = list(show = FALSE),
      axisLine = list(show = FALSE)
      
    ) %>% 
    e_legend(show = FALSE) %>% 
    e_flip_coords()

This is what I get

2

Any idea?

Upvotes: 0

Views: 416

Answers (1)

Quinten
Quinten

Reputation: 41307

You can use this to add the names on the bars:

Name <- c("Jon", "Bill", "Maria", "Ben", "Tina")
Age <- c(23, 41, 32, 58, 26)

df <- data.frame(Name, Age)

library(echarts4r)
library(dplyr)

df %>%
  e_chart(Name) %>%
  e_bar(Age, Name,
        label = list(show = TRUE, formatter = "{b}", position = "insideLeft")) %>%
  e_x_axis(
    inverse = TRUE,
    axisLabel = list(inside = TRUE),
    axisTick = list(show = FALSE),
    axisLine = list(show = FALSE)
  ) %>% 
  e_legend(show = FALSE) %>% 
  e_flip_coords()

enter image description here

Created on 2022-07-25 by the reprex package (v2.0.1)

old answer

You should use position = "right" in e_bar to label the values on your bars when using flipped coords like this:

Name <- c("Jon", "Bill", "Maria", "Ben", "Tina")
Age <- c(23, 41, 32, 58, 26)

df <- data.frame(Name, Age)

library(echarts4r)
library(dplyr)
df %>% 
  e_charts(Name) %>% 
  e_bar(Age,
        label = list(
          show = TRUE,
          position = "right"
        )) %>% 
  e_x_axis(
    inverse = TRUE,
    axisLabel = list(inside = TRUE),
    axisTick = list(show = FALSE),
    axisLine = list(show = FALSE)
  ) %>% 
  e_legend(show = FALSE) %>% 
  e_flip_coords()

enter image description here

Created on 2022-07-25 by the reprex package (v2.0.1)

Upvotes: 1

Related Questions