gpt4 book ai didi

r - echarts4r中如何指定坐标轴值的顺序

转载 作者:行者123 更新时间:2023-12-05 06:17:05 25 4
gpt4 key购买 nike

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

最佳答案

已经有一段时间了,但我正在回答这个问题。您可以使用重新排序参数(例如 e_chart(x,reorder=FALSE))。

在 OP 的辩护中,John Coene(echarts4r 包的所有者)在被问到这个问题后添加了这个功能:)

来源:https://github.com/JohnCoene/echarts4r/issues/126#issuecomment-585207460

关于r - echarts4r中如何指定坐标轴值的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61801355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com