'knitting rmarkdown throws error printing result of contour()
I want to knit the following code into an html document. The code runs fine in Rstudio but fails when I attempt to knit - what am I missing here?
library(e1071)
load(url("http://www.stanford.edu/~hastie/ElemStatLearn/datasets/ESL.mixture.rda"))
names(ESL.mixture)
attach(ESL.mixture)
plot(x,col=y+2,pch=c(17,19)[y+1],xlab=expression(x[1]),ylab=expression(x[2]),cex=1.5,cex.axis=1.5,cex.lab=1.5)
data <- data.frame(x, as.factor(y) )
colnames(data) <- c("x1", "x2", "y1")
attach(data)
svmfit <- svm(y1 ~ x1 + x2, data = data, kernel = "radial", cost = 32.1, gamma = 0.5, scale = FALSE)
xgrid <- expand.grid(x1=px1,x2= px2)
ygrid <- predict(svmfit,xgrid)
plot(x,col=y+2,pch=c(17,19)[y+1],xlab=expression(x[1]),ylab=expression(x[2]),cex=1.5,cex.axis=1.5,cex.lab=1.5)
points(xgrid,col=as.numeric(ygrid)+1,pch=20,cex=.2)
contour(px1,px2,matrix(ygrid,69,99),level=1,add=TRUE)
This produced the following plot as desired.
When attempting to knit it into a html report, it throws the following error. The dimensions of the data are definitely correct and those specified in the contour() call look fine as well, so am unclear on how to solve this.
Error in contour.default(px1, px2, matrix(ygrid, 69, 99), level = 1, add = TRUE) :
dimension mismatch
Calls: <Anonymous> ... eval_with_user_handlers -> eval -> eval -> contour -> contour.default
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|