plot.new error in R markdown

浪子不回头ぞ 提交于 2020-01-15 09:26:40

问题


I have some R code to plot an ellipse for a bivariate normal with known mean and variance using the ellipse() function from the mixtools package. However when I run this in Rmarkdown I get an error saying "plot.new has not been called yet". When I put another plot directly above it in the same chunk it runs but otherwise I get the error. What's the reason for this?

plot(ellipse(params,covariance, npoints = 500, alpha=0.01),
 xlim = c(-2,3.5),
 ylim = c(0,.75), xlab="alpha", ylab = "beta")

This code works fine when just run in R, the issue is only in markdown.


回答1:


mixtools function ellipse() offers a plot argument, look at the manual. So you can plot your ellipse like this:

ellipse(params, covariance, 
        npoints = 500, alpha=0.01, 
        newplot = TRUE, draw = TRUE, 
        xlim = c(-2,3.5), ylim = c(0,.75), 
        xlab="alpha", ylab = "beta")

The important arguments are newplot = TRUE and draw = TRUE. They offer you the plot of the ellipse and all other graphical parameters can be submitted to the function ellipse() via the three dot argument. If newplot = TRUE and draw = TRUE, plot the ellipse on a new plot. If newplot = FALSE and draw = TRUE, add the ellipse to an existing plot.



来源:https://stackoverflow.com/questions/39679329/plot-new-error-in-r-markdown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!