【问题标题】:Boundaries with ggplot2ggplot2 的边界
【发布时间】:2015-12-22 02:46:39
【问题描述】:

我使用 ggplot2 制作了以下图。如您所见,我有 3 个不同的类别,颜色为红色黑色蓝色。我想在两条边界上绘制两条曲线,将红点与黑点分开,将蓝点与黑点分开。我完全迷失了任何想法。

我的代码是:

datax=data.frame(x=y_data,y=x_data,
                 Diff_Motif_XY=factor(diff_motif,levels=c(1,0,‌​-1)),
                 size=factor(abs(diff_motif))) 
#
p=ggplot(datax,aes(x,y))+ 
     geom_point(aes(colour = Diff_Motif_XY,size=size))+ 
     xlab(cond2)+ 
     ylab(cond1)+ 
     scale_colour_manual(values=c("red","black","blue"))

【问题讨论】:

  • 你能发布一个代码示例吗?听起来您遇到了机器学习问题。
  • 这取决于你希望你的答案(你的曲线)看起来像一个数据结构。一种方法是构建一个分类器(使用类似 SVM 或其他东西),然后绘制 50% 等压线。我认为这将是相当简单的。我也能想到几个更简单的可能性,但它们不太通用。
  • 我正在寻找的曲线不应该是线性的,如果有的话,我更喜欢简单的曲线。我的代码是: datax=data.frame(x=y_data,y=x_data,Diff_Motif_XY=factor(diff_motif,levels=c(1,0,-1)),size=factor(abs(diff_motif))) p=ggplot (datax,aes(x,y))+ geom_point(aes(color = Diff_Motif_XY,size=size))+ xlab(cond2)+ ylab(cond1)+ scale_colour_manual(values=c("red","black","蓝色"))
  • 请将其添加到您的问题中。你可以编辑你知道的。
  • 请提供reproducible example

标签: r ggplot2


【解决方案1】:

我(太)好奇了。我认为边界看起来像是一条双曲线。可以使用optim 之类的方法计算最佳边界双曲线,但这将是一项相当大的工作量,而且可能不会收敛。

# Generate some data because the OP did not provide any

npts <- 30000
l_data <- pmax(0,runif(npts,-10,20))
s_data <- (20-l_data + 10)/6
xstar <- -5.1
ystar <- -5.1
x_data <- pmax(0,l_data + rnorm(npts,0,s_data)) + xstar
y_data <- pmax(0,l_data + rnorm(npts,0,s_data)) + ystar
ha <- 6.0
hb <- 6.0

xy2 <- ((x_data-xstar)/ha)^2 - ((y_data-ystar)/hb)^2 + 0.8*rnorm(npts)


diff_motif <- ifelse(xy2>1,1,ifelse(-xy2<1,0,-1))
cond1 <- ""
cond2 <- ""


# We need this to plot our hyperbola
genhyperbola <- function( cx,cy,a,b,u0,u1,nu,swap=F)
{
  # Generate a hyperbola through the parametric representation
  #  which uses sinh and cosh 
  #  We generate nu segements from u0 to u1
  #  swap just swaps the x and y axes allowing for a north-south hyperbola (swap=T)
  #
  #  https://en.wikipedia.org/wiki/Hyperbola
  #
  u <- seq(u0,u1,length.out=nu)
  x <- a*cosh(u)
  y <- b*sinh(u)
  df <- data.frame(x=x,y=y)
  df$x <- df$x + cx
  df$y <- df$y + cy
  if (swap){
    # for north-south hyperbolas
    tmp <- df$x
    df$x <- df$y
    df$y <- tmp
  }
  return(df)
}
hyp1 <- genhyperbola(xstar,ystar, ha,hb, 0,2.2,100, swap=F)
hyp2 <- genhyperbola(xstar,ystar, ha,hb, 0,2.2,100, swap=T)

datax=data.frame(x=x_data,y=y_data,
                 Diff_Motif_XY=factor(diff_motif,levels=c(1,0,-1)),
                 size=0) 

eqlab1 <- sprintf("((x+%.1f)/%.1f)^{2}-((y+%.1f)/%.1f)^{2} == 1",xstar,ha,ystar,hb)
eqlab2 <- sprintf("((y+%.1f)/%.1f)^{2}-((x+%.1f)/%.1f)^{2} == 1",ystar,hb,xstar,ha)
#
p=ggplot(datax,aes(x,y))+ 
  geom_point(aes(colour = Diff_Motif_XY),shape=".")+ 
  geom_path(data=hyp1,aes(x,y),color=I("purple"),size=1)+
  geom_path(data=hyp2,aes(x,y),color=I("brown"),size=1)+
  xlab(cond2)+ 
  ylab(cond1)+ 
  scale_colour_manual(values=c("blue","black","red")) +
  annotate('text', x=xstar+20, y=ystar+2,  
           label = eqlab1,parse = TRUE,size=6,color="purple") +
  annotate('text', x=xstar+5,  y=ystar+20, 
           label = eqlab2,parse = TRUE,size=6,color="brown") 

print(p)

这是图片:

【讨论】:

  • 抱歉,之前有错别字。
  • 嗨,迈克,感谢您的意见,这也是一个非常有趣的想法。我还开发了一个类似的局部凸包想法。但我也非常喜欢这个。感谢您的努力。
猜你喜欢
  • 1970-01-01
  • 2014-08-07
  • 2013-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-18
  • 2017-08-29
  • 2020-05-26
相关资源
最近更新 更多