如何在echarts4r中指定轴值的顺序?

问题描述 投票:0回答:1

我正试图用 echarts4r 制作一个条形图,但我在指定轴中的值的顺序时遇到了麻烦。

我想根据数字变量 "Casos "对名称("Estado "变量)进行排序(这是以递减顺序进行的)。我已经尝试了有序因子,但没有成功。

我可以使用以下方法产生正确的输出 ggplot + plotly 并与 highcharter.

有谁能帮我解决这个问题?

TIA

下面是我的MRE。

library(echarts4r)
library(magrittr)

my.df3 <- structure(list(Estado = c("São Paulo", "Rio de Janeiro", "Ceará",  "Pernambuco", "Amazonas", "Maranhão", "Pará", "Bahia", "Espírito Santo",  "Santa Catarina", "Minas Gerais", "Distrito Federal", "Rio Grande do Sul",  "Amapá", "Paraíba", "Alagoas", "Sergipe", "Rio Grande do Norte",  "Paraná", "Acre", "Piauí", "Rondônia", "Roraima", "Goiás",  "Tocantins", "Mato Grosso", "Mato Grosso do Sul"), Casos = c(51097, 18486, 18412, 14309, 14168, 9112, 9059, 6204, 5087, 3733, 3435, 2979, 2917, 2910, 2777, 2580, 2032, 1989, 1930, 1694, 1612, 1460, 1295, 1225, 828, 604, 405), Mortes = c(4118, 1928, 1280, 1157, 1098, 444, 914, 225, 212, 73, 127, 46, 111, 86, 154, 150, 37, 92, 116, 52, 57, 50, 24, 61, 14, 20, 12)), row.names = c(NA, -27L), class = c("tbl_df", "tbl", "data.frame"))

my.df3 %>%
  e_charts(x = Estado, elementId = "casos") %>%
  e_bar(Casos, legend = FALSE, name = "Casos") %>% 
  e_labels(position = "right") %>% 
  e_tooltip(
    trigger = "item",
    axisPointer = list(
      type = "line"
    )
  )  %>% 
  e_title("Casos Confirmados") %>% 
  e_flip_coords() %>% 
  e_y_axis(splitLine = list(show = FALSE), axisLabel = list(
    interval = 0L
  )) %>% 
  e_x_axis(show = FALSE) %>% 
  e_toolbox_feature(
    feature = "saveAsImage",
    title = "Save as image"
  ) -> echrtCasos


my.df3 %>%
  e_charts(x = Estado) %>%
  e_bar(Mortes, legend = FALSE, name = "Mortes") %>% 
  e_labels(position = "right") %>% 
  e_tooltip(
    trigger = "item",
    axisPointer = list(
      type = "line"
    )
  ) %>% 
  e_title("Mortes") %>% 
  e_flip_coords() %>% 
  e_y_axis(splitLine = list(show = FALSE), axisLabel = list(
    interval = 0L
  )) %>% 
  e_x_axis(show = FALSE) %>% 
  e_connect("casos") %>%
  e_toolbox_feature(
    feature = "saveAsImage",
    title = "Save as image"
  ) -> echrtMortes

e_arrange(echrtCasos, echrtMortes, rows = 1, cols = 2)

graph produced with above code

sessionInfo()的输出是。

R version 3.6.3 (2020-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.4 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.2.20.so

locale:
 [1] LC_CTYPE=pt_BR.UTF-8       LC_NUMERIC=C               LC_TIME=pt_BR.UTF-8        LC_COLLATE=pt_BR.UTF-8    
 [5] LC_MONETARY=pt_BR.UTF-8    LC_MESSAGES=pt_BR.UTF-8    LC_PAPER=pt_BR.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=pt_BR.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] echarts4r_0.2.3 magrittr_1.5   

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.4.6         rstudioapi_0.11      tidyselect_1.0.0     xtable_1.8-4         R6_2.4.1            
 [6] rlang_0.4.6          fastmap_1.0.1        dplyr_0.8.5          tools_3.6.3          clipr_0.7.0         
[11] htmltools_0.4.0.9000 ellipsis_0.3.0       yaml_2.2.1           assertthat_0.2.1     digest_0.6.25       
[16] tibble_3.0.1         lifecycle_0.2.0      crayon_1.3.4         shiny_1.4.0.2        purrr_0.3.4         
[21] later_1.0.0          htmlwidgets_1.5.1    vctrs_0.2.4          promises_1.1.0       glue_1.4.0          
[26] mime_0.9             compiler_3.6.3       pillar_1.4.4         jsonlite_1.6.1       httpuv_1.5.2        
[31] pkgconfig_2.0.3
r echarts
1个回答
0
投票

你是否尝试过 ggplot:

  my.df3 %>% 
      mutate(Estado = reorder(Estado, -Casos)) %>% 
      ggplot(aes(x=Estado, y=Casos))+
      geom_col()+
      coord_flip()

    my.df3 %>% 
      mutate(Estado = reorder(Estado, -Mortes)) %>% 
      ggplot(aes(x=Estado, y=Mortes))+
      geom_col()+
      coord_flip()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.