1

我正在尝试使用highcharter库在 R 中绘制条形图

我的数据框mostly_used看起来像这样:

        word    n
1        sir 8484
2       time 7339
3       miss 5954
4       dear 5422
5       hand 5305
6       head 4978
7      night 4240
8        day 4124
9       eyes 4040
10     house 4011

我使用以下代码行:

hchart(mostly_used, x = word, y = n, type = "column", name = "word count"
       , color = "blue")  %>% hc_add_theme(hc_theme_null())

我得到错误Error: Columns`x`, `y` must be 1d atomic vectors or lists

谁能解释这是为什么?

编辑:

> dput(mostly_used)
structure(list(word = c("sir", "time", "miss", "dear", "hand", 
"head", "night", "day", "eyes", "house"), n = c(8484L, 7339L, 
5954L, 5422L, 5305L, 4978L, 4240L, 4124L, 4040L, 4011L)), .Names = c("word", 
"n"), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))
4

1 回答 1

4

我查看了文档,发现示例没有使用您尝试过的语法。它看起来更实用。(我的努力在您进行包含数据示例的编辑之前,因此 mymostly_used只是一个普通的数据框,因此我使用as.character来强制我认为是一个因素。结果证明这是不必要的但无害的。)

我从机器上的小插图中获取了线型示例:http://localhost:13297/library/highcharter/doc/replicating-highcharts-demos.html并替换了相应的值:

highchart() %>% 
  hc_chart(type = "column") %>% 
  hc_title(text = "Counts of Mostly Used") %>% 
    hc_xAxis(categories = as.character(mostly_used$word)) %>% 
  hc_yAxis(title = list(text = "N")) %>% 
  hc_plotOptions(line = list(
    dataLabels = list(enabled = TRUE),
    enableMouseTracking = FALSE)
    ) %>% 
  hc_series(
    list(  name = "Used",
      data = mostly_used$n
    )
  )

这是我的 Chrome 会话中出现的屏幕截图:

在此处输入图像描述

您对另一篇文档的引用的评论表明,在 x 和 y 参数分配周围有一个hcaes-function。这对我有用:

hchart(mostly_used ,type = "column", title="Counts of Mostly Used",
                     hcaes( x = word, y=   n)    )

“为什么”是需要尊重该包对非标准评估的处理。它模拟了 ggplot2 包的策略,即使用“aes”函数来定义使用真实 R 名称的列名称,即在数据参数的上下文中评估的未引用标记。

于 2018-05-02T19:24:27.397 回答