Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reduced major axis line and CI for ggplot in R

Tags:

r

ggplot2

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: 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)
like image 300
Steve Avatar asked May 23 '11 21:05

Steve


2 Answers

Building off Joran's answer, I think it's a little easier to pass the whole data frame to geom_abline:

library(ggplot2)
library(lmodel2)

dat <- data.frame(a=log10(rnorm(50, 30, 10)), b=log10(rnorm(50, 20, 2)))
mod <- lmodel2(a ~ b, data=dat,"interval", "interval", 99)

reg <- mod$regression.results
names(reg) <- c("method", "intercept", "slope", "angle", "p-value")

ggplot(dat) + 
  geom_point(aes(b, a)) +
  geom_abline(data = reg, aes(intercept = intercept, slope = slope, colour = method))
like image 145
hadley Avatar answered Oct 05 '22 15:10

hadley


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))

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).

But maybe this will help you construct a working example in ggplot().

EDIT2: 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")
like image 33
joran Avatar answered Oct 05 '22 16:10

joran