减少R中ggplot的主轴线和CI [英] Reduced major axis line and CI for ggplot in R
问题描述
是否总有一条要减少的主轴线(最好是CI)添加到ggplot中?我知道我可以使用method ="lm"来获得OLS拟合,但是RMA似乎没有默认方法.我可以从包lmodel2中获得RMA系数和CI间隔,但是将它们与geom_abline()添加似乎不起作用.这是虚拟数据和代码.我只想用RMA行和CI替换OLS行和CI:
Is there anyway to add a reduced major axis line (and ideally CI) to a ggplot? I know I can use method="lm" to get an OLS fit, but there doesn't seem to be a default method for RMA. I can get the RMA coefs and the CI interval from package lmodel2, but adding them with geom_abline() doesn't seem to work. Here's dummy data and code. I just want to replace the OLS line and CI with a RMA line and CI:
dat <- data.frame(a=log10(rnorm(50, 30, 10)), b=log10(rnorm(50, 20, 2)))
ggplot(dat, aes(x=a, y=b) ) +
geom_point(shape=1) +
geom_smooth(method="lm")
Edit1:以下代码获取RMA(此处称为SMA-标准化主轴)的系数和CI.软件包lmodel2提供了更详细的输出,而软件包smatr仅返回coef和CI(如果有帮助的话):
the code below gets the RMA (here called SMA - standardized major axis) coefs and CIs. Package lmodel2 provides more detailed output, while package smatr returns just the coefs and CIs, if that's any help:
library(lmodel2)
fit1 <- lmodel2(b ~ a, data=dat)
library(smatr)
fit2 <- line.cis(b, a, data=dat)
推荐答案
正如Chase所说,您正在使用的实际 lmodel2()
代码和 ggplot
代码将是乐于助人.但是,以下示例可能会为您指明正确的方向:
As Chase commented, the actual lmodel2()
code and the ggplot
code you are using would be helpful. But here's an example that may point you in the right direction:
dat <- data.frame(a=log10(rnorm(50, 30, 10)), b=log10(rnorm(50, 20, 2)))
mod <- lmodel2(a ~ b, data=dat,"interval", "interval", 99)
#EDIT: mod is a list, with components (data.frames) regression.results and
# confidence.intervals containing the intercepts+slopes for different
# estimation methods; just put the right values into geom_abline
ggplot(dat,aes(x=b,y=a)) + geom_point() +
geom_abline(intercept=mod$regression.results[4,2],
slope=mod$regression.results[4,3],colour="blue") +
geom_abline(intercept=mod$confidence.intervals[4,2],
slope=mod$confidence.intervals[4,4],colour="red") +
geom_abline(intercept=mod$confidence.intervals[4,3],
slope=mod$confidence.intervals[4,5],colour="red") +
xlim(c(-10,10)) + ylim(c(-10,10))
完全公开:我对RMA回归一无所知,所以我只是使用了 lmodel2
中的一些示例代码,将相关事件斜率和截距拔出并将它们放入了 geom_abline()
中>作为指导.在这个玩具示例中生成的配置项似乎没有多大意义,因为我不得不强制ggplot使用 xlim()
和 ylim()
进行缩小参见CI线(红色).
Full disclosure: I know nothing about RMA regression, so I just plucked out the relevent slopes and intercepts and plopped them into geom_abline()
, using some example code from lmodel2
as a guide. The CIs produced in this toy example don't seem to make much sense, since I had to force ggplot to zoom out using xlim()
and ylim()
in order to see the CI lines (red).
但这也许可以帮助您在 ggplot()
中构建一个有效的示例.
But maybe this will help you construct a working example in ggplot()
.
在OP添加代码以提取系数的情况下, ggplot()
会像这样:
With OPs added code to extract the coefficients, the ggplot()
would be something like this:
ggplot(dat,aes(x=b,y=a)) + geom_point() +
geom_abline(intercept=fit2[1,1],slope=fit2[2,1],colour="blue") +
geom_abline(intercept=fit2[1,2],slope=fit2[2,2],colour="red") +
geom_abline(intercept=fit2[1,3],slope=fit2[2,3],colour="red")
这篇关于减少R中ggplot的主轴线和CI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!