2017-09-26 90 views
0

在stackoverflow上有幾個答案顯示如何返回迴歸線的公式,但我無法弄清楚如何讓他們工作並給我一個公式。在r中獲取迴歸線的公式

Here是我試圖使用的一個例子,它需要一個數據幀。

在我的代碼,我將數據從表中讀取:

vafs <- read.table("outputFile", header = TRUE) 
sample1 <- vafs$Sample1 
sample2 <- vafs$Sample2 

然後繪製一般是這樣的:

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

然後,如果我嘗試使用方法中的鏈接回答上面(也顯示在下面)我無法弄清楚如何正確地將它傳遞給我的數據;只是簡單地通過vafs不起作用。

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) 

下面是一些樣本輸入數據,以瞭解我作爲輸入:

Sample1 Sample2 Identity 
0.000100576639399 7.01336045166e-05 label 
0.000201153278798 0.000263732817381 label 
6.70510929328e-05 0.000109685906595 label 

回答

2

我發現它工作,如果我添加y,並且x參數給lm_eqn功能:

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

enter image description here

編輯:帶註釋我必須明確地定義xylm_eqn明確,因爲它們沒有在aes調用中定義。但是在外觀的改進是值得的:

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) 

enter image description here

+1

這是很好的做法與'註解來代替'geom_text(...)'(GEOM = 「文本」,...)' 。前者繪製每行數據一個實例,導致粗體/模糊的文本,而後者僅繪製一個標籤。 – Brian

+0

@missuse這實際上似乎幾乎工作,因爲一切都運行,我得到一個情節,但沒有公式顯示在劇情。我是否不正確地調用該方法? –

+1

@Brian謝謝,添加編輯與您的建議。它看起來好多了。夜總會(philly rock!)是否可以像我的例子那樣處理數據(汽車)?我正在使用ggplot2 2.2.1。在geom_text中,只有'x','y'和'label'在'aes'中才能使其工作。你能否提供一個dput數據樣本。 – missuse