英文:
how to create highchart with y axis with character vector
问题
我正在使用R包highcharter
创建这个高图表,但它不起作用,因为y轴是字符:
这是我的代码:
library(highcharter)
library(dplyr)
hchart(
mtcars %>% rownames_to_column("rowname"),
"scatter",
hcaes(x = mpg, y = rowname),
colorByPoint = TRUE
)
我该如何创建一个类似的图表?
英文:
I am creating this chart in highchart using the R package highcharter
but it’s not working because the y axis is a character:
This is my code:
library(highcharter)
library(dplyr)
hchart(
mtcars %>% rownames_to_column("rowname"),
"scatter",
hcaes(x = mpg, y = rowname),
colorByPoint = TRUE
)
How can I create a chart like this?
答案1
得分: 2
根据文档的描述,似乎在scatter
图中,无法在y轴上使用分类变量。不过,一个解决方法是将一个索引映射到y
,然后通过hc_yAsis
设置类别标签:
library(highcharter)
library(tibble)
library(dplyr)
y_axis_categories <- rownames(mtcars)
mtcars %>%
rowid_to_column("id") %>%
mutate(id = id - 1) %>%
hchart(
"scatter",
hcaes(x = mpg, y = id),
colorByPoint = TRUE
) %>%
hc_yAxis(categories = y_axis_categories)
英文:
As far as I get it from the docs we can't have a categorical variable on the y axis in case of scatter
. However, a workaround would be to map an index on y
then set the category labels via hc_yAsis
:
library(highcharter)
library(tibble)
library(dplyr)
y_axis_categories <- rownames(mtcars)
mtcars %>%
rowid_to_column("id") %>%
mutate(id = id - 1) |> # JS indexing starts at 0
hchart(
"scatter",
hcaes(x = mpg, y = id),
colorByPoint = TRUE
) %>%
hc_yAxis(categories = y_axis_categories)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论