2014-02-11 26 views
0

@Ista幫助我產生了以下代碼。從這段代碼我現在需要保存:從小平面ggplot2散點圖中提取原點和斜率值[R]

  • Q1:對於8個散點圖中的每一個,在nR = 0處的平均'm'值;
  • Q2:僅對於數據的一個子集(假設所有數據從nR = 0到nR = 50),迴歸的斜率(此處顯示爲stat_smooth)的值。正如您在示例中所看到的那樣,計算整個數據集的迴歸;
  • 數據應保存爲可用於其他可視化與ggplot2。我不確定這一點是否重要,但我提到它以防萬一。

require(reshape2) 
library(ggplot2) 
library(RColorBrewer) 

md = read.csv(file="http://dl.dropboxusercontent.com/u/73950/rob-136.csv", sep=",", header=TRUE) 

dM = melt(md,c("id")) 
# split variable out into its components 
dM <- cbind(dM, 
     colsplit(dM$variable, 
       pattern = "_", 
       names = c("Nm", "order", "category"))) 
# no longer need variable, as it is represented by the combination of Nm, order, and category 
dM$variable <- NULL 
# rearrange putting category in the columns 
dM <- dcast(dM, ... ~ category, value.var = "value") 

# plot 
p = ggplot(dM, aes(x=nR ,y=m)) 
p = p + scale_y_continuous(name="m")+ scale_x_continuous(name="nR") + xlim(0,136) 
p = p + facet_grid(order~Nm)+ ggtitle("Title") 
p = p + stat_bin2d(bins=50) 
myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral"))) 
p = p + scale_fill_gradientn(colours = myPalette(100)) 
p = p + theme(legend.position="none") 
p = p + stat_smooth(method = 'lm') 
p 

enter image description here

我希望這是非常明顯的,但是請讓我知道,如果我沒了感覺......

乾杯!

+0

幾點意見。爲什麼你一直把所有東西都分配給p並覆蓋它?做一個聲明,其中「圖層」由加號分隔。你是否考慮過在外面進行分析,然後通過特殊圖層('geom_line')引入結果(擬合值)?爲了提取統計結果,請參閱http://stackoverflow.com/questions/9789871/method-to-extract-stat-smooth-line-fit –

+0

我使用「p = p +」在需要時打開和關閉某些行。我覺得它很實用。實際上,我可以在其他地方進行計算,但是如果可能的話,我希望從新安排的dM數據集中一次完成。 – Rodolphe

+0

我可能加快了自己。 James和Brian提出的解決方案不輸出模型參數,只是預測和錯誤。 –

回答

1

我會這樣做。

myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral"))) 

# plot 
ggplot(dM, aes(x = nR, y = m)) + 
    scale_y_continuous(name = "m") + 
    scale_x_continuous(name = "nR") + 
    xlim(0, 136) + 
    facet_grid(order ~ Nm) + 
    ggtitle("Title") + 
    stat_bin2d(bins = 50) + 
    scale_fill_gradientn(colours = myPalette(100)) + 
    theme(legend.position="none") + 
    stat_smooth(method = "lm") 

out <- by(data = dM, INDICES = list(dM$order, dM$Nm), FUN = function(x) { 
    model <- lm(m ~ nR, data = x) 
    data.frame(
    intercept = coef(model)["(Intercept)"], 
    nR = coef(model)["nR"], 
    nM = paste(unique(x$Nm), "/", unique(x$order), sep = "") 
    ) 
}) 
do.call("rbind", out) 

       intercept   nR  nM 
(Intercept) 0.07883183 0.0190254723 FS/GED 
(Intercept)1 0.11683689 0.0007879976 FS/NAR 
(Intercept)2 0.14945769 0.0036112481 N2/GED 
(Intercept)3 0.16427558 0.0017356170 N2/NAR 
(Intercept)4 0.48951709 0.0025201291 SW/GED 
(Intercept)5 0.51569334 0.0011353692 SW/NAR 
(Intercept)6 0.65299500 0.0012065830 VE/GED 
(Intercept)7 0.64931290 -0.0001557808 VE/NAR 

編輯

或者,你可以引入第二個任期到模型方程,併產生「曲線美」曲線。

ggplot(dM, aes(x = nR, y = m)) + 
    scale_y_continuous(name = "m") + 
    scale_x_continuous(name = "nR") + 
    xlim(0, 136) + 
    facet_grid(order ~ Nm) + 
    ggtitle("Title") + 
    stat_bin2d(bins = 50) + 
    scale_fill_gradientn(colours = myPalette(100)) + 
    theme(legend.position="none") + 
    stat_smooth(method = "lm", formula = y ~ poly(x, 2)) 

out <- by(data = dM, INDICES = list(dM$order, dM$Nm), FUN = function(x) { 
    x <- na.omit(x) 
    model <- lm(m ~ poly(nR, 2), data = x) 
    coef(model) # you will need to explicitly add where the data has come from, see my original post above 
}) 
do.call("rbind", out) 

    (Intercept) poly(nR, 2)1 poly(nR, 2)2 
[1,] 0.2137094 1.9243960 0.36091764 
[2,] 0.1585034 1.7404097 -0.10095676 
[3,] 0.2945133 5.2416769 1.54143778 
[4,] 0.2569599 3.8638288 0.95676753 
[5,] 0.5992618 4.0801451 1.38542308 
[6,] 0.5752421 2.4440650 0.08027183 
[7,] 0.7034230 1.8407400 -0.20495917 
[8,] 0.6411417 -0.3364163 -0.02258523 

enter image description here

+0

哇,這是NICE @羅曼,非常感謝!你認爲你的方法可以調整以獲得線性函數(現在順序線性可以)只能達到nR = 50? – Rodolphe

+0

你是什麼意思'線性函數',@Rodolphe?一旦你有'x'項的截距和係數('poly(nR,2)'),你就可以計算擬合的值(線)到你想要的任何'nR'範圍。甚至超出用來適應生產線的範圍(危險)。 –

+0

哦,當然,我的意思是擬合不應該考慮所有數據集,而應該考慮一個子集(其中nR值不超過50)。在「model < - lm(m〜nR,data = x)」這條線上,它可能是類似於「m〜(nR <= 50)」的東西嗎? – Rodolphe