用ggplot2绘制决策边界? [英] Plot decision boundaries with ggplot2?

查看:375
本文介绍了用ggplot2绘制决策边界?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用ggplot2绘制 contour (base R)的等价物?下面是一个线性判别函数分析的例子:

pre $ $ $ $ $ $ $ $ $ $ iris.lda <-lda(物种〜Sepal.Length + Sepal.Width + Petal.Length + Petal.Width,数据=虹膜)
datPred< -data.frame(物种=预测(iris.lda)$类,预测(iris.lda)$ x)#create data.frame

#Base R plot
eqscplot(datPred [,2],datPred [,3],pch = as.double(datPred [,1])) col = as.double(datPred [,1])+ 1)

#创建判定边界
iris.lda2 < - lda(datPred [,2:3],datPred [ 1])$ ​​b $ bx < - seq(min(datPred [,2]),max(datPred [,2]),length.out = 30)
y < - seq(min(datPred [ 3)),max(datPred [,3]),length.out = 30)
Xcon< - 矩阵(c(rep(x,length(y)),
rep(y,rep (length(x),length(y)))),, 2)#设置网格中所有可能的x和y对

iris.pr1 < - predict(iris.lda2,Xcon )$ post [,c(setosa,versicolor)]%*%c(1,1)#属于每个类的点的后验概率
轮廓(x,y,矩阵(iris.pr1 ,l (x),长度(y)),
levels = 0.5,add = T,lty = 3,method =simple)#绘制基本R图中的轮廓线
iris.pr2< ; - 预测(iris.lda2,Xcon)$ post [,c(virginica,setosa)]%*%c(1,1)
contour(x,y,matrix(iris.pr2,长度(x),长度(y)),
等级= 0.5,add = T,lty = 3,method =简单)

#等价图与ggplot2但没有决策边界
ggplot(datPred,AES(X = LD1,Y = LD2,COL =物种))+
geom_point(尺寸= 3,AES(PCH =物种))

使用ggplot绘制轮廓线时不可能使用矩阵。矩阵可以使用融合重新排列成数据帧。在下面的数据框中,来自iris.pr1的概率值显示在第一列以及以下两列中的x和y坐标中。 x和y坐标形成30 x 30点的网格。 (熔化(矩阵(iris.pr1,长度(x),长度(y))),其中, x = x [X1],y = y [X2])[, - c(1,2)]

我想绘制后验概率为0.5(即判定边界)的坐标(最好通过平滑曲线连接)。

解决方案

您可以在ggplot中使用 geom_contour 来达到类似的效果。正如你正确的假设,你必须改变你的数据。我最终只是在做

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

然后,您可以将该data.frame传递给geom_contour,并指定您希望在0.5处使用

  ggplot(datPred,AES(X = LD1,Y = LD2))+ 
geom_point(尺寸= 3,AES(PCH =物种,COL =物种))+
geom_contour(数据= PR,AES(X = X,Y = Y,Z = Z1),断裂= C(0,0.5))+
geom_contour(数据= PR,AES(X = x,y = y,z = z2),break = c(0,.5))

并给出了


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

require(MASS)
iris.lda<-lda(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, data =    iris)
datPred<-data.frame(Species=predict(iris.lda)$class,predict(iris.lda)$x) #create data.frame

#Base R plot
eqscplot(datPred[,2],datPred[,3],pch=as.double(datPred[,1]),col=as.double(datPred[,1])+1) 

#Create decision boundaries
iris.lda2 <- lda(datPred[,2:3], datPred[,1])
x <- seq(min(datPred[,2]), max(datPred[,2]), length.out=30)
y <- seq(min(datPred[,3]), max(datPred[,3]), length.out=30)
Xcon <- matrix(c(rep(x,length(y)),
             rep(y, rep(length(x), length(y)))),,2) #Set all possible pairs of x and y  on a grid

iris.pr1 <- predict(iris.lda2, Xcon)$post[, c("setosa","versicolor")] %*% c(1,1)    #posterior probabilities of a point belonging to each class 
contour(x, y, matrix(iris.pr1, length(x), length(y)), 
    levels=0.5, add=T, lty=3,method="simple") #Plot contour lines in the base R plot
iris.pr2 <- predict(iris.lda2, Xcon)$post[, c("virginica","setosa")] %*% c(1,1)
contour(x, y, matrix(iris.pr2, length(x), length(y)), 
    levels=0.5, add=T, lty=3,method="simple") 

#Eqivalent plot with ggplot2 but without decision boundaries
ggplot(datPred, aes(x=LD1, y=LD2, col=Species) ) + 
geom_point(size = 3, aes(pch = Species))

It is not possible to use a matrix when plotting contour lines with ggplot. The matrix can be rearranged to a data-frame using melt. In the data-frame below the probability values from iris.pr1 are displayed in the first column along with the x and y coordinates in the following two columns. The x and y coordinates form a grid of 30 x 30 points.

df <- transform(melt(matrix(iris.pr1, length(x), length(y))), x=x[X1], y=y[X2])[,-c(1,2)]

I would like to plot the coordinates (preferably connected by a smoothed curve) where the posterior probabilities are 0.5 (i.e. the decision boundaries).

解决方案

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

这篇关于用ggplot2绘制决策边界?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆