2016-06-23 13 views
2

私は、参加者を示すそれらの間の線を持つすべてのデータポイントをプロットするのが好きです。ここでは、私が条件と刺激の種類に応じてプロットした私の参加者の評価のそれぞれ持っている:私が欲しいものすべてのデータをgeom_pointとしてプロットし、ggplot2の手段を示す行を含む。 stat_summaryの問題

WHAT I HAVE

は、各状態の色に刺激の種類ごとに条件ごとの平均の行を追加することです。理想的には、これは次のようになります。

WHAT I NEED

私はggplot2ドキュメントサイトhereに説明するようstat_summaryとstat_sum_dfを使用してみましたが、私はそれを動作させることはできません。それは何もしません、またはそれはすべての単一の参加者のための行をプロットします。

私は最初のグラフを生成するために使用されるコードは次のとおりです。

ggplot(df, aes(x=StimulusType+jitterVal, y=Rating, group=ParticipantCondition)) + 
    geom_point(size=4.5, aes(colour=Condition), alpha=0.3)+ 
    geom_line(size=1, alpha=0.05)+ 
    scale_y_continuous(limits=c(0, 7.5), breaks=seq(0,7,by=1))+ 
    scale_colour_manual(values=c("#0072B2", "#009E73", "#F0E442", "#D55E00"))+ 
    xlab('Stimulus type') + 
    scale_x_continuous(limits=(c(0.5, 2.5)), breaks = c(0.9, 1.9), labels = levels(df$StimulusType))+ 
    ylab('Mean Rating') + 
    guides(colour = guide_legend(override.aes = list(alpha = 1))) + 
    theme_bw() 

...と、以下のようにあなたが最初の4人の参加者のための例のデータフレームを作成することができます。

Participant <- rep(c("01", "02", "03", "04"), 8) 
StimulusType <- rep(rep(c(1, 2), each=4), 4) 
Condition <- rep(c("A", "B", "C", "D"), each=8) 
Rating <- c(5.20, 5.55, 3.10, 4.05, 5.05, 5.85, 3.90, 5.25, 4.70, 3.15, 3.40, 4.85, 4.90, 4.00, 3.95, 3.95, 3.00, 4.60, 3.95, 4.00, 3.15, 5.20, 
5.05, 3.70, 2.75, 3.40, 4.80, 4.55, 2.35, 2.45, 5.45, 4.05) 
jitterVal <- c(-0.19459509, -0.19571169, -0.17475060, -0.19599276, -0.17536634, -0.19429345, -0.17363951, -0.17446702, -0.13601392, 
-0.14484280, -0.12328058, -0.12427593, -0.12913823, -0.12042329, -0.14703381, -0.12603936, -0.09125372, -0.08213296, 
-0.09140868, -0.09728309, -0.08377205, -0.08514802, -0.08715795, -0.08932001, -0.02689549, -0.04717990, -0.03918013, 
-0.03068255, -0.02826789, -0.02345827, -0.03473678, -0.03369023) 

df <- data.frame(Participant, StimulusType, Condition, Rating, jitterVal) 
ParticipantCondition <- paste(df$Participant, df$Condition) 

私は各条件ごとに各参加者のポイント間の線を取得するために作成したグループ化変数のParticipantConditionで問題が発生している可能性があると考えてください。

ご協力いただければ幸いです。

答えて

2

あなたがグループ化の問題を回避するために開始する前にサマリーを生成する必要があるかもしれません。 1つのオプションは次のとおりです。 enter image description here

:あなたが示したように多くのプロットを与える

library(dplyr) 
summaryData <- 
    df %>% 
    group_by(StimulusType, Condition) %>% 
    summarise(meanRating = mean(Rating) 
      , jitterVal = mean(jitterVal)) %>% 
    mutate(xmin = StimulusType+jitterVal-0.04 
     , xend = StimulusType+jitterVal+0.04) 

ggplot(df, aes(x=StimulusType+jitterVal, y=Rating, group=ParticipantCondition)) + 
    geom_point(size=4.5, aes(colour=Condition), alpha=0.3)+ 
    geom_line(size=1, alpha=0.05)+ 
    scale_y_continuous(limits=c(0, 7.5), breaks=seq(0,7,by=1))+ 
    scale_colour_manual(values=c("#0072B2", "#009E73", "#F0E442", "#D55E00"))+ 
    xlab('Stimulus type') + 
    scale_x_continuous(limits=(c(0.5, 2.5)), breaks = c(0.9, 1.9), labels = levels(df$StimulusType))+ 
    ylab('Mean Rating') + 
    guides(colour = guide_legend(override.aes = list(alpha = 1))) + 
    geom_segment(data = summaryData 
       , mapping = aes(x=xmin 
           , xend=xend 
           , y=meanRating 
           , yend =meanRating 
           , group = NA 
           , colour = Condition) 
       , lwd = 3 
       , show.legend = FALSE 
) + 
    theme_bw() 

2

dplyrを使用して平均値を計算しました。平均値は、2乗で表されます。あなたはこのことについてどう思いますか?

library(dplyr) 
library(ggplot2) 
Participant <- rep(c("01", "02", "03", "04"), 8) 
StimulusType <- rep(rep(c(1, 2), each=4), 4) 
Condition <- rep(c("A", "B", "C", "D"), each=8) 
Rating <- c(5.20, 5.55, 3.10, 4.05, 5.05, 5.85, 3.90, 5.25, 4.70, 3.15, 3.40, 4.85, 4.90, 4.00, 3.95, 3.95, 3.00, 4.60, 3.95, 4.00, 3.15, 5.20, 
      5.05, 3.70, 2.75, 3.40, 4.80, 4.55, 2.35, 2.45, 5.45, 4.05) 
jitterVal <- c(-0.19459509, -0.19571169, -0.17475060, -0.19599276, -0.17536634, -0.19429345, -0.17363951, -0.17446702, -0.13601392, 
       -0.14484280, -0.12328058, -0.12427593, -0.12913823, -0.12042329, -0.14703381, -0.12603936, -0.09125372, -0.08213296, 
       -0.09140868, -0.09728309, -0.08377205, -0.08514802, -0.08715795, -0.08932001, -0.02689549, -0.04717990, -0.03918013, 
       -0.03068255, -0.02826789, -0.02345827, -0.03473678, -0.03369023) 

df <- data.frame(Participant, StimulusType, Condition, Rating, jitterVal) 
ParticipantCondition <- paste(df$Participant, df$Condition) 
rm(Rating, StimulusType, Condition, jitterVal) 

levels(df$Condition) 

mean_values <- df %>% group_by(StimulusType ,Condition) %>% select(Rating, jitterVal) %>% summarise_each(funs(mean)) 
mean_values <- ungroup(mean_values) 
levels(mean_values$Condition) <- levels(df$Condition) 

ggplot(df, aes(y=Rating, x = StimulusType + jitterVal)) + 
    geom_point(size=4.5, aes(colour = Condition), alpha=0.4) + 
    geom_line(size=1, alpha=0.05, aes(group = ParticipantCondition)) + 
    geom_rect(data = mean_values, 
      aes(xmin = ((StimulusType + jitterVal) - 0.05), 
       xmax = ((StimulusType + jitterVal) + 0.05), 
       ymin = Rating - 0.05, 
       ymax = Rating + 0.05, 
       fill = Condition)) + 
    scale_y_continuous(limits=c(0, 7.5), breaks=seq(0,7,by=1))+ 
    scale_colour_manual(values=c("#0072B2", "#009E73", "#F0E442", "#D55E00"))+ 
    scale_fill_manual(values=c("#0072B2", "#009E73", "#F0E442", "#D55E00"))+ 
    xlab('Stimulus type') + 
    scale_x_continuous(limits=(c(0.5, 2.5)), breaks = c(0.9, 1.9), labels = levels(df$StimulusType))+ 
    ylab('Mean Rating') + 
    guides(colour = guide_legend(override.aes = list(alpha = 1))) + 
    theme_bw() 

矩形のサイズはもちろん簡単に調整できます。

enter image description here

関連する問題