为什么我 "plot.new has not been called yet" 在 chartJSRadar() 之后调用 legend()

Why am I getting "plot.new has not been called yet" calling legend() after chartJSRadar()

这里的上下文是用于统计计算的 R 项目

考虑以下代码(来自 chartJSRadar 文档)

library(radar chart)
labs <- c("Communicator", "Data Wangler", "Programmer",
      "Technologist",  "Modeller", "Visualizer")

scores <- list(
  "Rich" = c(9, 7, 4, 5, 3, 7),
  "Andy" = c(7, 6, 6, 2, 6, 9),
  "Aimee" = c(6, 5, 8, 4, 7, 6)
)

chartJSRadar(scores = scores, labs = labs, maxScale = 10)

我想在使用中添加一个图例:

legend("topleft", c("Rich","Andy","Aimee"), cex=0.8, fill=colors)

但我收到以下错误:

Error in strwidth(legend, units = "user", cex = cex, font = text.font) : 
  plot.new has not been called yet

我已经看到许多关于此错误消息的其他问题。其中一些是因为图例坐标超出了图表。其他一些人给出了解决方法,但没有解释为什么会出现错误。

我的问题是:为什么会出现此错误?这似乎是一些根本性的错误,比如 legend() 和 chartJSRadar() 之间的不兼容,但我不明白。

请指教!

似乎chartJSRadar 没有调用情节,而是在查看器中显示交互式情节。因此不会调用绘图,您不能使用 legend()。从 chartJSRadar 的文档来看,雷达图似乎有一个图例选项,请参阅 http://www.chartjs.org/docs/#radar-chart-chart-options:

//String - A legend template
legendTemplate : "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].strokeColor%>\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>"

这是 JavaScript,我对此并不专业,所以很遗憾,我只能帮到你了。

尝试使用具有 radarchart() 功能的 fmsb 包。您可以为您的数据绘制相同的图。

http://127.0.0.1:25882/library/fmsb/html/radarchart.html

我在 R 中尝试过,它在 R plot window 中有效。由于我不了解雷达图数据。您肯定可以使用 radarchart()legend() 使用包 fmsb

来完成图例

如果使用 R Studio,请确保 运行 整个块(而不是 ctr + enter)。我在使用 igraph 时遇到了类似的问题。我试图将图例附加到图表上,但出现了相同的错误消息。 运行 整个块都有效。