sta*_*tar 5 statistics r bioinformatics ggplot2
我有一个散点图,我想知道如何在置信区间线上方和下方找到基因?
编辑:可重复的例子:
library(ggplot2)
#dummy data
df <- mtcars[,c("mpg","cyl")]
#plot
ggplot(df,aes(mpg,cyl)) +
geom_point() +
geom_smooth()
Run Code Online (Sandbox Code Playgroud)
这个解决方案利用了ggplot2为您做的辛勤工作:
library(sp)
# we have to build the plot first so ggplot can do the calculations
ggplot(df,aes(mpg,cyl)) +
geom_point() +
geom_smooth() -> gg
# do the calculations
gb <- ggplot_build(gg)
# get the CI data
p <- gb$data[[2]]
# make a polygon out of it
poly <- data.frame(
x=c(p$x[1], p$x, p$x[length(p$x)], rev(p$x)),
y=c(p$ymax[1], p$ymin, p$ymax[length(p$x)], rev(p$ymax))
)
# test for original values in said polygon and add that to orig data
# so we can color by it
df$in_ci <- point.in.polygon(df$mpg, df$cyl, poly$x, poly$y)
# re-do the plot with the new data
ggplot(df,aes(mpg,cyl)) +
geom_point(aes(color=factor(in_ci))) +
geom_smooth()
Run Code Online (Sandbox Code Playgroud)
它需要一些调整(即最后一点得到一个2值),但我的时间有限.请注意,point.in.polygon返回值为:
0:点是pol的外部1:点是pol的内部2:点位于pol边缘的相对内部3:point是pol的顶点所以应该很容易将代码更改为TRUE/ FALSE是否值0.
我不得不深入了解github回购,但我终于得到了它.为了做到这一点,你需要知道如何stat_smooth工作.在这个具体案例中loess调用函数进行平滑(可以使用与下面相同的过程构造不同的平滑函数):
所以,loess在这种情况下我们会这样做:
#data
df <- mtcars[,c("mpg","cyl"), with=FALSE]
#run loess model
cars.lo <- loess(cyl ~ mpg, df)
Run Code Online (Sandbox Code Playgroud)
然后我必须阅读这个,以便了解预测是如何在内部进行的stat_smooth.显然,hadley使用predictdf函数(未导出到命名空间),如下所示:
predictdf.loess <- function(model, xseq, se, level) {
pred <- stats::predict(model, newdata = data.frame(x = xseq), se = se)
if (se) {
y = pred$fit
ci <- pred$se.fit * stats::qt(level / 2 + .5, pred$df)
ymin = y - ci
ymax = y + ci
data.frame(x = xseq, y, ymin, ymax, se = pred$se.fit)
} else {
data.frame(x = xseq, y = as.vector(pred))
}
}
Run Code Online (Sandbox Code Playgroud)
阅读完上述内容后,我可以使用以下方法创建自己的数据预测框架:
#get the predictions i.e. the fit and se.fit vectors
pred <- predict(cars.lo, se=TRUE)
#create a data.frame from those
df2 <- data.frame(mpg=df$mpg, fit=pred$fit, se.fit=pred$se.fit * qt(0.95 / 2 + .5, pred$df))
Run Code Online (Sandbox Code Playgroud)
观察predictdf.loess我们可以看到置信区间的上边界被创建为pred$fit + pred$se.fit * qt(0.95 / 2 + .5, pred$df),下边界被创建为pred$fit - pred$se.fit * qt(0.95 / 2 + .5, pred$df).
使用那些我们可以为这些边界之上或之下的点创建一个标志:
#make the flag
outerpoints <- +(df$cyl > df2$fit + df2$se.fit | df$cyl < df2$fit - df2$se.fit)
#add flag to original data frame
df$outer <- outerpoints
Run Code Online (Sandbox Code Playgroud)
该df$outer列可能是OP正在寻找的(如果它在边界之外则取值为1或者否则为0)但是为了它,我正在下面绘制它.
请注意,+上面的函数仅用于将逻辑标志转换为数字.
现在,如果我们绘制如下:
ggplot(df,aes(mpg,cyl)) +
geom_point(aes(colour=factor(outer))) +
geom_smooth()
Run Code Online (Sandbox Code Playgroud)
我们实际上可以看到置信区间内外的点.
输出:
PS对于任何对上下边界感兴趣的人,他们都是这样创造的(推测:虽然阴影区域可能是用geom_ribbon创建的 - 或类似的东西 - 这使得它们更圆而且漂亮):
#upper boundary
ggplot(df,aes(mpg,cyl)) +
geom_point(aes(colour=factor(outer))) +
geom_smooth() +
geom_line(data=df2, aes(mpg , fit + se.fit , group=1), colour='red')
#lower boundary
ggplot(df,aes(mpg,cyl)) +
geom_point(aes(colour=factor(outer))) +
geom_smooth() +
geom_line(data=df2, aes(mpg , fit - se.fit , group=1), colour='red')
Run Code Online (Sandbox Code Playgroud)
使用ggplot_build类似@ hrbrmstr的漂亮解决方案,您实际上可以通过简单地传递一系列x值来geom_smooth指定应该计算错误界限的位置,并使其等于点的x值.然后,您只需查看y值是否在范围内.
library(ggplot2)
## dummy data
df <- mtcars[,c("mpg","cyl")]
ggplot(df, aes(mpg, cyl)) +
geom_smooth(params=list(xseq=df$mpg)) -> gg
## Find the points within bounds
bounds <- ggplot_build(gg)[[1]][[1]]
df$inside <- with(df, bounds$ymin < cyl & bounds$ymax > cyl)
## Add the points
gg + geom_point(data=df, aes(color=inside)) + theme_bw()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1271 次 |
| 最近记录: |