R ggplot2:我如何绘制一个隐式函数(只有一个级别的轮廓线)? [英] R ggplot2: how do I plot an implicit function (contour line at just one level)?

查看:388
本文介绍了R ggplot2:我如何绘制一个隐式函数(只有一个级别的轮廓线)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用R,我想绘制一些数据点和它们的曲线(这是用神经网络训练的决策边界)。首先,我用普通的plot函数做了它,但现在我希望它看起来更加花哨,用 ggplot2



这就是没有ggplot时的样子(注意曲线特别是点不太相关): imgur.com/IJf3N.pngalt =Image>



使用 ggplot 绘制点是没问题的,但现在我想添加一条曲线,它由以下函数描述:

  1.91 *(1 /( 1 + exp( - (23.50 + 12.64 * x-24.54 * y)))) -  1.95 *(1 /(1 + exp( - (73.51-12.36 * x- 10.01 * y))))+ 0.98 = b $ b  

这是一个只有两个变量等于零的函数,例如3x-2y = 0。正如你所看到的,这个很难在ay = ...形式中重写,所以我想通过在等级= 0处使用等高线图来绘制等式。
它使用curve3d我的散点图:

  curve3d(1.91 *(1 /(1 + exp( - (23.50 + 12.64 * x-24.54 * y )))) -  1.95 *(1 /(1 + exp( - (73.51-12.36 * x-10.01 * y))))+ 0.98 = 0,sys3d =contour,levels = 0,add = TRUE)

现在我真的想用 ggplot2 来实现相同的结果。我已经尝试了 stat_contour 图表,但它们似乎没有采用函数,并且不允许只选择一个级别。所以:


  1. 有没有什么办法用方程形式绘制函数(如ax + by = 0但可能更复杂) code> ggplot ?

  2. 我可以在geom_point ggplot 中添加这样一条曲线数据点?

编辑:这是重新创建我的数据的代码示例:

 #data 
x1 < - rorm(200,3,28)
y1 < - rorm(200,3, (100,3.35,.15)
x2 < - rnorm(100,3.45,.15)
y2 < - rorm(100,3.35,.15) ,15)
y3 < - rnorm(100,2.4,.15)

组<-c(rep(H,200),rep(A, (x1,x2,x3),y = c(y1,y2,y3),group = groups)

#工作ggplot
windows()
ggplot(data,aes(x = x,y = y))+ xlim(2,4)+ ylim(2, 4)+ geom_point(aes(color = group))+ scale_shape_manual(values = c(1,16))

#我想用ggplot绘制的旧情节(1.50 *(1 /(1 + exp( - (23.50 + 12.64 * x-24.54 * y)))) - 1.95 *(1 /(1 +(exp( - (73.51-12.36 * x-10.01 * y))))+ 0.98,xlim = c(2,4),ylim = c(2,4),sys3d =contour,levels = 0,add = TRUE)

因此,对于这些数据,我想绘制 1.91 *(1 /(1 + EXP( - (23.50 + 12.64 * X-24.54 * Y)))) - 1.95×(1 /(1 + EXP( - (73.51-12.36 * X-10.01 * Y))) )+ 0.98 = 0 (或任何其他隐式函数,如 5x + 2y = 0 ,而不必重写它)。

解决方案

tl; dr 您真正缺少的唯一东西是 breaks = 0
Base plot:

  g0 < -  ggplot(data,aes(x = x,y = y)) + xlim(2,4)+ ylim(2,4)+ 
geom_point(aes(color = group))+ scale_shape_manual(values = c(1,16))

生成轮廓数据(不绘制任何东西):

 <$ c (1+(1+(1+ exp( - (23.50 + 12.64 * x-24.54 * y)))) -  
1.95 *(1 / exp( - (73.51-12.36 * x-10.01 * y))))+ 0.98,
xlim = c(2,4),ylim = c(2,4),sys3d =none)

将数据重组为数据框:

<$ p $ (cc $ z)< - 列表(cc $ x,cc $ y)
mm< - reshape2 :: melt(cc $ z)
< code>

借助 breaks = 0

  g0 + geom_contour(data = mm,
aes(x = Var1,y = Var2,z = value),breaks = 0,
color =black)


Using R, I wanted to plot some datapoints and a curve over them (which is the decision boundary trained with neural network). First I did it with the normal plot functions, but now I want it to look more fancy using ggplot2.

This is what it looks like without ggplot (note the curve especially, the points are not too relevant):

Plotting the points with ggplot is no problem, but now I want to add the curve as well, which is described by the following function:

1.91*(1/(1+exp(-(23.50+12.64*x-24.54*y))))-1.95*(1/(1+exp(-(73.51-12.36*x-10.01*y)))) + 0.98 = 0

This is a just function with two variables equated to zero, like 3x-2y=0 for example. As you can see this one is difficult to rewrite in a y=... form, so that is why I would like to plot the equation by using a contour plot at level = 0. It worked using curve3d on top of my scatterplot:

curve3d(1.91*(1/(1+exp(-(23.50+12.64*x-24.54*y))))-1.95*(1/(1+exp(-(73.51-12.36*x-10.01*y)))) + 0.98 = 0, sys3d="contour",levels=0, add=TRUE)

Now I really want to use ggplot2 to achieve the same result. I've tried the stat_contour plots, but they don't seem to take functions and will not allow to choose only one level. So:

  1. Is there any way to plot a function in equation form (like ax+by=0 but probably more complex) form using ggplot?
  2. Can I add such a plotted curve to my geom_point ggplot with datapoints?

EDIT: here is a code example recreating my data:

# data
x1 <- rnorm(200,   3,    .28)
y1 <- rnorm(200,   3,    .28)
x2 <- rnorm(100, 3.45, .15)
y2 <- rnorm(100, 3.35, .15)
x3 <- rnorm(100, 3.3,  .15)
y3 <- rnorm(100, 2.4,  .15)

groups <- c(rep("H",200), rep("A",100), rep("B",100))
data <- data.frame(x = c(x1,x2,x3), y = c(y1,y2,y3), group = groups)

# the working ggplot
windows()
ggplot(data, aes(x=x,y=y)) +  xlim(2,4) + ylim(2,4) + geom_point(aes(color = group)) + scale_shape_manual(values=c(1,16))

# the old plot that I would like to plot with ggplot over the previous one with as well (doesn't work)
curve3d(1.91*(1/(1+exp(-(23.50+12.64*x-24.54*y))))-1.95*(1/(1+exp(-(73.51-12.36*x-10.01*y)))) + 0.98, xlim=c(2,4), ylim=c(2,4), sys3d="contour",levels=0, add=TRUE)

So with this data, I would like to plot the function 1.91*(1/(1+exp(-(23.50+12.64*x-24.54*y))))-1.95*(1/(1+exp(-(73.51-12.36*x-10.01*y)))) + 0.98 = 0 (or any other implicit function, like 5x+2y=0 without having to rewrite it).

解决方案

tl;dr the only thing you were really missing was breaks=0. Base plot:

g0 <- ggplot(data, aes(x=x,y=y)) +  xlim(2,4) + ylim(2,4) +
    geom_point(aes(color = group)) + scale_shape_manual(values=c(1,16))

Generate data for contour (without drawing anything):

cc <- emdbook::curve3d(1.91*(1/(1+exp(-(23.50+12.64*x-24.54*y))))-
              1.95*(1/(1+exp(-(73.51-12.36*x-10.01*y)))) + 0.98,
              xlim=c(2,4), ylim=c(2,4), sys3d="none")

Reorganize data into a data frame:

dimnames(cc$z) <- list(cc$x,cc$y)
mm <- reshape2::melt(cc$z)

Draw with breaks=0

g0 + geom_contour(data=mm,
                  aes(x=Var1,y=Var2,z=value),breaks=0,
                  colour="black")

这篇关于R ggplot2:我如何绘制一个隐式函数(只有一个级别的轮廓线)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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