Reputation: 5769
There are a couple of answers on stackoverflow that show how to return a formula for a regression line, but I cannot figure out how to get them to work and give me a formula.
Here is one example that I am trying to use that takes a dataframe.
In my code, I am reading in data from a table:
vafs <- read.table("outputFile", header = TRUE)
sample1 <- vafs$Sample1
sample2 <- vafs$Sample2
And then plotting generally like this:
lm_eqn <- function(df,y,x){
m <- lm(y ~ x, df);
eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,
list(a = format(coef(m)[1], digits = 2),
b = format(coef(m)[2], digits = 2),
r2 = format(summary(m)$r.squared, digits = 3)))
as.character(as.expression(eq));
}
p <- ggplot(vafs, aes(x=sample1, y=sample2, alpha=0.5, label=identity, size=15)) +
geom_text(aes(x = 10, y = 300, label = lm_eqn(vafs, sample1, sample2)), parse = TRUE)+
geom_point() +
xlim(0,0.003) +
ylim(0,0.003) +
geom_abline(intercept = 0, slope = 1, size=3)+ # y=x line
xlab('\nVAF Individual 1') + ylab('VAF Individual 2\n') +
labs(title = 'Muliplier = 1x\n')+
geom_smooth(method=lm, se=TRUE, size=3, colour='red')+ # regression line
theme_bw()+ # no gray background
theme(panel.border = element_blank())+ # no border
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+ # no gridlines
theme(axis.title = element_text(size = 50))+ # change label size
theme(plot.title = element_text(size = 50))+ # change title size
theme(plot.title = element_text(hjust = 0.5))+ # center title
theme(axis.text.x = element_text(size = 50, colour="black", angle=90))+ # change tick size
theme(axis.text.y = element_text(size = 50, colour="black"))+ # change tick size
theme(legend.position="none")+ # no legend
theme(axis.ticks = element_line(colour = "black", size = 2))+ # hide ticks
theme(axis.line = element_line(colour = "black", size=3)) # add axis
jpeg("output2.jpg", units="in", width=17, height=17, res=500)
print(p)
dev.off()
Then if I try to use the method answered in the link above (also shown below) I can't figure out how to properly pass it my data; just simply passing vafs does not work.
lm_eqn <- function(df){
m <- lm(y ~ x, df);
eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,
list(a = format(coef(m)[1], digits = 2),
b = format(coef(m)[2], digits = 2),
r2 = format(summary(m)$r.squared, digits = 3)))
as.character(as.expression(eq));
}
p1 <- p + geom_text(x = 25, y = 300, label = lm_eqn(df), parse = TRUE)
Here is some sample input data to understand what i'm taking as input:
Sample1 Sample2 Identity
0.000100576639399 7.01336045166e-05 label
0.000201153278798 0.000263732817381 label
6.70510929328e-05 0.000109685906595 label
Upvotes: 0
Views: 215
Reputation: 19756
I found it working if I add y
, and x
arguments to the lm_eqn
function:
library(ggplot2)
ggplot(cars)+
geom_point(aes(x=speed, y =dist ))+
geom_text(aes(x = 10, y = 300, label = lm_eqn(cars, dist, speed)), parse = TRUE)
lm_eqn <- function(df, y, x){
m <- lm(y ~ x, df);
eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,
list(a = format(coef(m)[1], digits = 2),
b = format(coef(m)[2], digits = 2),
r2 = format(summary(m)$r.squared, digits = 3)))
as.character(as.expression(eq));
}
EDIT: with annotate I had to define x
and y
in lm_eqn
explicitly since they were not defined in the aes
call. However the improvement in the look is worth it:
ggplot(cars)+
geom_point(aes(x=speed, y =dist ))+
annotate(geom = "text",x = 10, y = 300, label = lm_eqn(cars, cars$dist, cars$speed), parse = TRUE)
Upvotes: 2