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

时间:2020-05-14 15:32:35

标签: r echarts

我正在尝试使用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

1 个答案:

答案 0 :(得分: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