2016-07-14 4 views
1

私は2つの連続変数(小胞および細胞)と、2つのレベル(HC及びRA)を有する単一のグループ化変数で設定単純なデータは、ここでシミュレートした:グループ内の非線形回帰直線とggplot2の合計データをプロットする方法は?

###Simulate Vesicle variable### 
Vesicle.hc <- sort(runif(23, 0.98, 5)) #HC group 
Vesicle1.ra <- sort(runif(5, 0.98, 3)) #RA group 
Vesicle <- c(Vesicle.hc, Vesicle1.ra) #Combined 

###Simulate Cells variable### 
z <- seq(23) 
Cells.hc <- (rnorm(23, 50 + 30 * z^(0.2), 8))*runif(1, 50000, 400000) #HC group 
Cells.ra <- c(8.36e6, 6.35e6, 1.287e7, 1.896e7, 1.976e7)    #RA group 
Cells <- c(Cells.hc, Cells.ra)           #Combined 

###Define groups and create dataframe### 
Group <- rep("HC",23)        #HC group 
Group1 <- rep("RA",5)        #RA Group 
Group <- c(Group, Group1)       #Combined 
df <- data.frame(Cells, Vesicle, Group)    #Data frame 

Iはデータの散布図をプロットしました使用して、個別に各グループに取り付けられた(hereを示す)、非線形回帰ラインとggplot2を、使用して:

###Plot data### 
library(ggplot2) 
ggplot(df, aes(x = Cells, y = Vesicle, colour=Group)) + 
    xlab("Stimulated neutrophils") + 
    ylab("MV/cell") + 
    stat_smooth(method = 'nls', formula = 'y~a*exp(b*x)',      #Fit nls model 
       method.args = list(start=c(a=0.1646, b=9.5e-8)), se=FALSE) + #Starting values 
    geom_point(size=4, pch=21,color = "black", stroke=1.5, aes(fill=Group)) #Change point style 

私の質問はどのように私もプロットすることができ、各グループの非線形回帰関数をプロットに加えて、あります回帰直線はに適合データ、すなわち、グループ化変数の寄与を無視したデータのモデル化?

+1

あなたはCells.hc' 'の作成であり、予想外のシンボルを持っています。また、 'z'がどこに作られるのかわかりません。 –

+0

別の 'stat_smooth'を' aes(group = NULL) 'で追加してください。 – Axeman

+0

ああ私の誠実なご謝謝、私は私の仕事の例を作ったとき、私の環境でそれらの変数を既に持っていた - 私はどれほど怠惰です。編集されたコードが機能するはずです。私はあなたの提案@Axemanを試しましたが、エラーはスローされませんが、プロットに変更はありません。 – Hefin

答えて

0
ggplot(df, aes(x = Cells, y = Vesicle, colour=Group)) + 
    xlab("Stimulated neutrophils") + 
    ylab("MV/cell") + 
    stat_smooth(method = 'nls', formula = 'y~a*exp(b*x)', 
       method.args = list(start=c(a=0.1646, b=9.5e-8)), se=FALSE) + 
    stat_smooth(color = 1, method = 'nls', formula = 'y~a*exp(b*x)', 
       method.args = list(start=c(a=0.1646, b=9.5e-8)), se=FALSE) + 
    geom_point(size=4, pch=21,color = "black", stroke=1.5, aes(fill=Group)) 

enter image description here

+0

美しいありがとう。つまり、 "color = 1"を追加するだけで、stat_smoothがグループ分類を継承することを防ぐことができます。 – Hefin

+0

そうですね。 'inherit.aes'引数もありますが、これは' x'と 'y'を再度定義したことを意味します。グループ化を無効にする方が簡単なことがよくあります。 – Axeman