简体   繁体   English

条形图中部分或全部隐藏的标签 (echarts4r)

[英]Labels partially or totally hidden in bar chart (echarts4r)

I use the echarts4r package in R.我在 R 中使用 echarts4r package。 When I run the code below, I get a bar chart where the x-axis labels (countries' names) are partially hidden.当我运行下面的代码时,我得到一个条形图,其中 x 轴标签(国家名称)被部分隐藏。 The last bar displaying 18000 is for South Africa, but it is completely hidden.最后一个显示 18000 的柱是南非的,但它是完全隐藏的。 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)代表 package (v2.0.1) 于 2022 年 7 月 30 日创建

Playing with the z value solves the issue (as was already solved here ):使用 z 值解决了这个问题(这里已经解决):

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)     

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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