Plot decision boundaries with ggplot2?

后端 未结 2 1876
[愿得一人]
[愿得一人] 2020-12-15 09:44

How do I plot the equivalent of contour (base R) with ggplot2? Below is an example with linear discriminant function analysis:

require(MASS)
iri         


        
2条回答
  •  有刺的猬
    2020-12-15 10:36

    You can use geom_contour in ggplot to achieve a similar effect. As you correctly assumed, you do have to transform your data. I ended up just doing

    pr<-data.frame(x=rep(x, length(y)), y=rep(y, each=length(x)), 
        z1=as.vector(iris.pr1), z2=as.vector(iris.pr2))
    

    And then you can pass that data.frame to the geom_contour and specify you want the breaks at 0.5 with

    ggplot(datPred, aes(x=LD1, y=LD2) ) + 
        geom_point(size = 3, aes(pch = Species,  col=Species)) + 
        geom_contour(data=pr, aes(x=x, y=y, z=z1), breaks=c(0,.5)) + 
        geom_contour(data=pr, aes(x=x, y=y, z=z2), breaks=c(0,.5))
    

    and that gives

    ggplot with probability contour boundaries

提交回复
热议问题