I'm trying to draw a barplot in R using the highcharter
library
My dataframe mostly_used
looks something like this:
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
and I use the following line of code:
hchart(mostly_used, x = word, y = n, type = "column", name = "word count"
, color = "blue") %>% hc_add_theme(hc_theme_null())
I get the error Error: Columns`x`, `y` must be 1d atomic vectors or lists
can anyone explain why this is?
edit:
> 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"
))
I looked at the documentation and see the examples didn't use the syntax you tried. It appeared more functional. (My efforts preceded your edit that included the data example, so my mostly_used
was just an ordinary dataframe, hence my use of as.character
to coerce what I thought would be a factor. That turned out to be unnecessary but harmless.)
I took the line-type example from the vignette on my machine: http://localhost:13297/library/highcharter/doc/replicating-highcharts-demos.html and replaced the corresponding values:
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
)
)
This is a screenshot of what appeared in my Chrome session:
Your comment with the citation to a different bit of documentation showed that there was an hcaes
-function wrapped around the x and y argument assignments. This worked for me:
hchart(mostly_used ,type = "column", title="Counts of Mostly Used",
hcaes( x = word, y= n) )
The "why" is the need to respect that package's handling of non-standard evaluation. It emulates the ggplot2 package's strategy of using an "aes"-function to define the column names using real R names, i.e. unquoted tokens that are evaluated in the context of the data argument.