如何使用字符向量创建具有字符向量的高图表的y轴

huangapple go评论80阅读模式
英文:

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)

如何使用字符向量创建具有字符向量的高图表的y轴

英文:

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 &lt;- rownames(mtcars)

mtcars %&gt;% 
  rowid_to_column(&quot;id&quot;) %&gt;%
  mutate(id = id - 1) |&gt; # JS indexing starts at 0
  hchart(
  &quot;scatter&quot;,
  hcaes(x = mpg, y = id),
  colorByPoint = TRUE
) %&gt;%  
  hc_yAxis(categories = y_axis_categories)

如何使用字符向量创建具有字符向量的高图表的y轴

huangapple
  • 本文由 发表于 2023年2月20日 00:23:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501579.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定