简体   繁体   中英

Labels partially or totally hidden in bar chart (echarts4r)

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)

在此处输入图像描述

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)

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)     

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM