2016-04-13 12 views
2

私はいくつかの粘性実験に取り組んでおり、ν対θのアイリングプロットを作ろうとしています。 ggplot2でプロットを作成すると、モデルが表示されません。geom_smoothがggplotに表示されない

これらは、使用している値であり:

ここ
> theta 
[1] 25 30 35 40 45 
> nu 
[1] 1.448462 1.362730 1.255161 1.167408 1.083005 

私は上記の私の値でプロットを作成します。

plot <- 
    ggplot()+ 
    geom_point(mapping = aes(theta, nu), colour = "#0072bd", size = 4, shape = 16)+ 
    theme_bw()+ 
    labs(
    x = expression(paste(theta, " ", "[°C]")), 
    y = expression(paste("ln(", nu, ")", " ", "[mPa*s]")))+ 
    ylim(0, 10)+ 
    xlim(0, 100) 

That's what the plot looks like.

さて、私はgeom_smooth()

と私のモデルを追加します
plot + 
    geom_smooth(
    method = "nls", 
    method.args = list(formula = nu~a*exp(b/theta), 
         start=list(a=1, b=0.1))) 

しかし、何も起こりません...エラーメッセージでも、プロットはまったく同じように見えます。

誰もが私は間違いを見つけることができます私もgeom_smooth()引数と開始値だけでなく、

plot + 
    geom_smooth(
    method = "nls", 
    formula = nu~a*exp(b/theta), 
    start=list(a=1, b=0.1)) 

直接formulaを入れてみましたが、その後、私は

Error:Unknown parameter: start

を取得作る?

ありがとうございます!

EDIT

乾杯美学マッピングを分離し、

plot <- 
    ggplot()+ 
    aes(theta, nu)+ 
    geom_point(colour = "#0072bd", size = 4, shape = 16)+ 
    theme_bw()+ 
    labs(
    x = expression(paste(theta, " ", "[°C]")), 
    y = expression(paste("ln(", nu, ")", " ", "[mPa*s]")))+ 
    ylim(0, 10)+ 
    xlim(0, 100) 

私は次のエラーを取得する(まだ何も変わりません):

警告メッセージ:

1: In min(x) : no non-missing arguments to min; returning Inf 2: In max(x) : no non-missing arguments to min; returning -Inf 3: Computation failed in stat_smooth(): $ operator is invalid for atomic vectors

+1

あなたがggplotの外 'nls'をしようとするとどうなりますか? – oshun

+0

@oshun 'nls'は外に動作するようです: '> NLS(NU〜*のEXP(B /シータ)は、開始=リスト(A = 1、B = 0.1)) ' は私に次のようになります: '非線形回帰モデル モデル:NU〜*のEXP(B /シータ) データ:parent.frame() AB 0.6153 22.7767 残差和の二乗:収束までの反復0.008136 番号:5 達成されたコンバージェンス許容誤差:3.11e-06' –

+1

あなたの美学マッピングを 'geom_point'に含めるのではなく、別々に置いてください。' ggplot()+ aes(theta、nu)+ geom_point(color = "#0072b d "、サイズ= 4、形状= 16)+ ...。その後、エラーメッセージが表示されます。 – lukeA

答えて

4

あなたはコメントで指摘されたそれらの多くが起こっていくつかのことを、持っています。

あなたがグローバルggplot内または各geom内のいずれかの美学をggplotためdata.frameであなたの変数を入れて、あなたを定義したら、上に行く主なものはgeom_smoothの数式を使用して、yxの代わりに参照するために期待していることです変数名。 geom_smoothは、yxにマッピングした変数をaesに使用します。

その他の問題は、hereです。predict.nlsから標準エラーが得られないので、geom_smoothse = FALSEを使用する必要があります。ここで

は、あなたのgeom_smoothコードがどのように見えるかです:

geom_smooth(method = "nls", se = FALSE, 
       method.args = list(formula = y~a*exp(b/x), start=list(a=1, b=0.1))) 

そしてここで、完全なコードおよびプロットです。

ggplot(df, aes(theta, nu))+ 
    geom_point(colour = "#0072bd", size = 4, shape = 16)+ 
    geom_smooth(method = "nls", se = FALSE, 
       method.args = list(formula = y~a*exp(b/x), start=list(a=1, b=0.1))) + 
    theme_bw()+ 
    labs(
     x = expression(paste(theta, " ", "[°C]")), 
     y = expression(paste("ln(", nu, ")", " ", "[mPa*s]")))+ 
    ylim(0, 10) + 
    xlim(0, 100) 

enter image description here あなたはデフォルトの代わりにfullrange = TRUEを使用しない限りgeom_smoothは、データセットの範囲外に合わないことに注意してください。あなたが5つのデータポイントしか持っていなければ、これはかなり疑わしいかもしれません。

ggplot(df, aes(theta, nu))+ 
    geom_point(colour = "#0072bd", size = 4, shape = 16)+ 
    geom_smooth(method = "nls", se = FALSE, fullrange = TRUE, 
       method.args = list(formula = y~a*exp(b/x), start=list(a=1, b=0.1))) + 
    theme_bw()+ 
    labs(
     x = expression(paste(theta, " ", "[°C]")), 
     y = expression(paste("ln(", nu, ")", " ", "[mPa*s]")))+ 
    ylim(0, 10) + 
    xlim(0, 100) 

enter image description here

+0

'geom_smooth'に関する非常に有益な情報! – oshun

2

@lukeAがこのコメントを書いたので、この回答を書いたばかりです。

df<- data.frame(theta = c(25, 30, 35, 40, 45), 
       nu = c(1.448462, 1.362730, 1.255161, 1.167408, 1.083005)) 

myModel <- nls(nu~a*exp(b/theta), data=df, start=list(a=1, b=0.1)) 

myPredict <- expand.grid(theta = seq(5, 100, by =0.1)) 
    #expand.grid here in case your model has more than one variable 
    #Caution, extrapolating well beyond the data 
myPredict$fit <- predict(myModel, newdata= myPredict) 


plot + geom_line(data = myPredict, aes(x= theta, y= fit)) 

enter image description here

関連する問題