0

I use the echarts4r package in R. When I run the code below, I get a bar chart where the x-axis labels (countries' names) are partially hidden. The last bar displaying 18000 is for South Africa, but it is completely hidden. Is it possible to make the countries' names visible without changing their positions on the chart?

library(echarts4r) 
country <-  c("Côte d'Ivoire", "Papua New Guinea", "Republic of Venezuela", "The United Kingdom ", "South Africa")
quantity <- c(2000, 500, 1800,  2500, 18000)
df <- data.frame(country, quantity)

df %>% 
  e_charts(country) %>%
  e_bar(quantity,
         label = list(show = TRUE, 
                      position = "insideEnd", 
                      offset = c(535, 17)),
        showBackground = TRUE,
        itemStyle = list(color = "#490B3D")
  ) %>%
  e_x_axis(
    axisTick = list(show = FALSE),
    axisLine =list(show = FALSE),
    axisLabel = list(show = TRUE, inside = TRUE),
    inverse =  TRUE
  ) %>% 
  e_y_axis(show = FALSE) %>% 
  e_flip_coords() %>% 
  e_legend(show = FALSE)

enter image description here

thelatemail
  • 91,185
  • 12
  • 128
  • 188

2 Answers2

0

You can use the following code:

library(echarts4r) 
library(dplyr)
country <-  c("Côte d'Ivoire", "Papua New Guinea", "Republic of Venezuela", "The United Kingdom ", "South Africa")
quantity <- c(2000, 500, 1800,  2500, 18000)
df <- data.frame(country, quantity)

df %>%
  e_chart(country) %>%
  e_bar(quantity, country,
        label = list(show = TRUE, formatter = "{b}", position = "insideLeft", itemStyle = list(color = "#490B3D"))) %>%
  e_x_axis(
    axisTick = list(show = FALSE),
    axisLine =list(show = FALSE),
    axisLabel = list(show = FALSE, inside = TRUE),
    inverse =  TRUE
  ) %>% 
  e_y_axis(show = FALSE) %>% 
  e_legend(show = FALSE) %>% 
  e_flip_coords()

![](https://i.imgur.com/HZ7Z4MP.png)

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

Quinten
  • 35,235
  • 5
  • 20
  • 53
0

Playing with the z value solves the issue (as was already solved here):

library(echarts4r)
library(dplyr)

country <-  c("Côte d'Ivoire", "Papua New Guinea", "Republic of Venezuela", "The United Kingdom ", "South Africa")
quantity <- c(2000, 500, 1800,  2500, 18000)

df <- data.frame(country, quantity)

df %>% 
  e_charts(country) %>%
  e_bar(quantity,
         label = list(show = TRUE, 
                      position = "insideEnd", 
                      offset = c(535, 17)),
        showBackground = TRUE,
        itemStyle = list(color = "#490B3D"),
        z = 1
  ) %>%
  e_x_axis(
    axisTick = list(show = FALSE),
    axisLine =list(show = FALSE),
    axisLabel = list(show = TRUE, inside = TRUE),
    inverse =  TRUE,
    z = 2
  ) %>% 
  e_y_axis(show = FALSE) %>% 
  e_flip_coords() %>% 
  e_legend(show = FALSE)     
rdatasculptor
  • 8,112
  • 14
  • 56
  • 81