简体   繁体   English

为什么在chartJSRadar()之后调用legend()导致“ plot.new尚未被调用”

[英]Why am I getting “plot.new has not been called yet” calling legend() after chartJSRadar()

The context here is the R Project for Statistical Computing 这里的上下文是用于统计计算的R项目

Consider the following code (from chartJSRadar documentation) 考虑以下代码(来自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)

I want to put a legend on in using: 我想在使用中画上一个传说:

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

But I get the following error: 但是我收到以下错误:

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

I've seen many other questions about this error message. 我已经看到有关此错误消息的许多其他问题。 Some of them was because legend coordinates were off the chart. 其中一些是因为图例坐标不在图表中。 Some others give a workaround but do not explain why the error occur. 其他一些人提供了一种解决方法,但没有解释错误发生的原因。

My question is: why am I getting this error? 我的问题是:为什么我会收到此错误? It seems something fundamentally wrong like an incompatibility between legend() and chartJSRadar(), but I don't get it. 似乎从根本上来说是错误的,例如legend()和chartJSRadar()之间的不兼容,但是我不明白。

Please advise! 请指教!

If using R Studio make sure to run the entire chunk (not ctr + enter). 如果使用R Studio,请确保运行整个块(而不是ctr + enter)。 I had a similar issue using igraph. 我使用igraph遇到类似的问题。 I tried to attach the legend to the graph but had the same error message. 我试图将图例附加到图形上,但有相同的错误消息。 Running the entire chunk worked. 运行整个块工作。

It seems chartJSRadar does not call a plot but shows an interactive plot in the viewer. 似乎chartJSRadar不会调用图,而是在查看器中显示一个交互式图。 Hence a plot is not called and you cannot use legend() . 因此,不会调用图,并且您不能使用legend() From the documentation for chartJSRadar , it does seem like there is a legend option for the radar plot, see http://www.chartjs.org/docs/#radar-chart-chart-options : 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>"

This is JavaScript, which I have no expertise in, so unfortunately this is as far as I can help you. 这是JavaScript,我不擅长,所以很遗憾,这是我能为您提供的帮助。

Try using fmsb package which has radarchart() function. 尝试使用具有radarchart()函数的fmsb软件包。 You can plot the same for your data. 您可以为数据绘制相同的图形。

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

I tried it in R and it works in R plot window. 我在R中尝试过,它在R绘图窗口中有效。 As I do not understand the radar chart data. 由于我不了解雷达图数据。 You would definitely be able to do it with legend using radarchart() and legend() using package fmsb 您绝对可以通过使用radarchart()和使用包fmsb legend()使用legend()进行fmsb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM