2017-06-05 5 views
1

5つの処理(コントロールを含む)から数ヶ月の期間にわたって何らかのデータが記録されています。私は時系列としてデータをプロットするためにggplotを使用しており、生データの手段と各日付の標準エラーのデータフレームを生成しています。ggplotを使用して複数のデータ系列にエラーバーを追加する

私は5つのすべての治療を同じグラフにプロットして、エラーバーを表示しようとしています。私はa)できるよ。 1つの処置群をプロットし、エラーバーおよびb)を示す。 5つすべての治療をプロットしますが、エラーバーは表示されません。

はここ

 dates c_mean_am c_se_am T1_mean_am T1_se_am 
1 2017-01-31 284.135 27.43111  228.935  23.39037  
2 2017-02-09 226.944 13.08237  173.241  13.42946  
3 2017-02-23 281.135 15.89709  252.665  20.73417 
4 2017-03-14 265.655 15.29930  238.225  17.47501 
5 2017-04-06 312.785 13.08237  237.485  13.42946 
  • c_mean_am =コントロールがコントロール
  • T1_mean_am =治療の1のための
  • c_se_am =標準誤差を意味します(私はここでしかきちんと物事を保つために2回の治療を含めました)私のデータをです手段
  • T1_se_am =治療1の標準誤差

は、ここで私はエラーバーを取得できますかオプションA)

ggplot(summary, aes(x=dates, y=c_mean_am),xlab="Date") + 
    geom_point(shape = 19, size = 2,color="blue") + 
    geom_line(color="blue") + 
    geom_errorbar(aes(x=dates, ymin=c_mean_am-c_se_am, ymax=c_mean_am+c_se_am), color="blue", width=0.25) 

here's the plot

上記ここでオプションBのためのコードがあります)

sp <- ggplot(summary,aes(dates,y = Cond,color=Treatment)) + 
    geom_line(aes(y = c_mean_am, color = "Control")) + 
    geom_line(aes(y = T1_mean_am, color = "T1")) + 
    geom_point(aes(y = c_mean_am, color = "Control")) + 
    geom_point(aes(y = T1_mean_am, color = "T1")) 

sp2<- sp + 
    scale_color_manual(breaks = c("Control", "T1","T2"), values=c("blue", "yellow")) 

sp2 

here's the plot

上記を達成するために私のコードです2番目のプロットでは、ポイントとラインと同じ色を使用しますか?

おかげ

AB

答えて

1

は、最初の長い形式にデータを変換:

df <- df %>% 
gather(mean_type, mean_val, c_mean_am, T1_mean_am) %>% 
gather(se_type, se_val, c_se_am, T1_se_am) 


ggplot(df, aes(dates, mean_val, colour=mean_type)) + 
    geom_line() + 
    geom_point() + 
    geom_errorbar(aes(ymin=mean_val-se_val, ymax=mean_val+se_val)) 

enter image description here

編集:説明tidyr操作のため

new.dat <- mtcars %>% # taking mtcars as the starting data.frame 
     select(gear, cyl, mpg, qsec) %>% 
      # equivalent to mtcars[, c("gear", "cyl", "mpg", "qsec")]; to simplify the example 
     gather(key=type, value=val, gear, cyl) %>% 
      # convert the data into a long form with 64 rows, with new factor column "type" and numeric column "val". "gear" and "cyl" are removed while "mpg" and "qsec" remain 

new.dat[c(1:3, 33:35),] 

#  mpg qsec type val 
# 1 21.0 16.46 gear 4 
# 2 21.0 17.02 gear 4 
# 3 22.8 18.61 gear 4 
# 33 21.0 16.46 cyl 6 
# 34 21.0 17.02 cyl 6 
# 35 22.8 18.61 cyl 4 

ロングフォームのデータでは、新しい識別子フォーム(「タイプ」)をプロット目的で使用することができます。

ggplot(new.dat, aes(val, mpg, fill=type)) + 
    geom_col(position="dodge") 

enter image description here

長フォーマットは、例えば、異なる面上にプロットするために有用です迅速な応答のための

ggplot(new.dat, aes(val, mpg, colour=type)) + 
    geom_point() + 
    facet_wrap(~type) 

enter image description here

+0

感謝。あなたのコードを使用して、次のエラーメッセージが表示されます。「エラー:is.character(x)がTRUEではありません」 私は間違っていますか? –

+0

あなたのdata.frameの構造は何ですか?そして、どのステップでエラーが発生しましたか?データ操作またはプロット? –

+0

私はRにはかなり新しいので、これで私を導く必要があるかもしれません。データフレームの構造はどういう意味ですか? 私は基本的にあなたのコードをコピーし、私のデータ操作の直後に配置してプロットを生成し、エラーを受け取りました。私は "tidyr"と "devtools"パッケージをインストールする必要がありました。 –

関連する問題