highcharts 在R highcharter包中添加字符向量,因为y轴不工作

pwuypxnk  于 2023-03-18  发布在  Highcharts
关注(0)|答案(1)|浏览(177)

我一直试图在R中使用highchartpackaga创建这个简单的图表,但它不起作用。
我不知道是否必须将对象的类改为因子。
有人帮忙吗?

library(gapminder)
library(highcharter)
df <- gapminder::gapminder %>%
  filter(continent == 'Americas', year == 2007)

df%>%

  hchart(
    'scatter',
    names = 'SQUADS',
    hcaes(y = country,
          x = factor(lifeExp)
    ),
    color = '#B55C64'
    # colorByPoint = TRUE
  ) %>%

  hc_yAxis(

    type = 'category',
    categories = df$country,
    tickAmount = 20,
    gridLineWidth = 0.5,
    gridLineColor = 'gray',
    gridLineDashStyle = "longdash",

    title = list(text = ''))
tvz2xvvm

tvz2xvvm1#

here类似,您可以使用因子列的levels,并将列的索引设置为0,如下所示:

library(highcharter)
library(dplyr)
df <- gapminder::gapminder %>%
  filter(continent == 'Americas', year == 2007)

your_ylabels = levels(df$country)

df %>%
  hchart('scatter',
         hcaes(x = lifeExp, y = as.numeric(country) - 1)) %>%
  hc_yAxis(categories = your_ylabels)

创建于2023年3月17日,使用reprex v2.0.2

相关问题