Plot decision boundaries with ggplot2?

后端 未结 2 1874
[愿得一人]
[愿得一人] 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

    0 讨论(0)
  • 2020-12-15 10:38

    The partimat function in the klaR library does what you want for observed predictors, but if you want the same for the LDA projections, you can build a data frame augmenting the original with the LD1...LDk projections, then call partimat with formula Group~LD1+...+LDk, method='lda' - then you see the "LD-plane" that you intended to see, nicely partitioned for you. This seemed easier to me, at least to explain to students newer to R, since I'm just reusing a function already provided in a way in which it wasn't quite intended.

    0 讨论(0)
提交回复
热议问题