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

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

how to create highchart with y axis with character vector

问题

我正在使用R包highcharter创建这个高图表,但它不起作用,因为y轴是字符:

这是我的代码:

  1. library(highcharter)
  2. library(dplyr)
  3. hchart(
  4. mtcars %>% rownames_to_column("rowname"),
  5. "scatter",
  6. hcaes(x = mpg, y = rowname),
  7. colorByPoint = TRUE
  8. )

我该如何创建一个类似的图表?

英文:

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:

  1. library(highcharter)
  2. library(dplyr)
  3. hchart(
  4. mtcars %>% rownames_to_column("rowname"),
  5. "scatter",
  6. hcaes(x = mpg, y = rowname),
  7. colorByPoint = TRUE
  8. )

How can I create a chart like this?

答案1

得分: 2

根据文档的描述,似乎在scatter图中,无法在y轴上使用分类变量。不过,一个解决方法是将一个索引映射到y,然后通过hc_yAsis设置类别标签:

  1. library(highcharter)
  2. library(tibble)
  3. library(dplyr)
  4. y_axis_categories <- rownames(mtcars)
  5. mtcars %>%
  6. rowid_to_column("id") %>%
  7. mutate(id = id - 1) %>%
  8. hchart(
  9. "scatter",
  10. hcaes(x = mpg, y = id),
  11. colorByPoint = TRUE
  12. ) %>%
  13. 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:

  1. library(highcharter)
  2. library(tibble)
  3. library(dplyr)
  4. y_axis_categories &lt;- rownames(mtcars)
  5. mtcars %&gt;%
  6. rowid_to_column(&quot;id&quot;) %&gt;%
  7. mutate(id = id - 1) |&gt; # JS indexing starts at 0
  8. hchart(
  9. &quot;scatter&quot;,
  10. hcaes(x = mpg, y = id),
  11. colorByPoint = TRUE
  12. ) %&gt;%
  13. 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:

确定