Reputation: 111
I'm trying to produce a bar graph with echarts4r but I'm having trouble to specify the order of the values in the axis.
I would like to sort the names ("Estado" variable) according to numerical variable "Casos" (this in decreasing order). I've tried ordered factors without success.
I was able to produce correct output using ggplot
+ plotly
and also with highcharter
.
Any help with this?
TIA
Below is my 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)
The output of sessionInfo() is:
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
Upvotes: 3
Views: 2398
Reputation: 465
It has been a while but I am answering it for the record. You can use reorder parameter (e.g. e_chart(x,reorder=FALSE)
).
In OP's defense, John Coene (owner of echarts4r package) added this feature after this question was asked:)
Source: https://github.com/JohnCoene/echarts4r/issues/126#issuecomment-585207460
Upvotes: 1
Reputation: 892
Did you try 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()
Upvotes: -1