2016-06-23 52 views
2

我喜歡繪製所有數據點與他們之間的線表示參與者。在這裏,我有我的每一個參與者的評級繪製的根據條件和刺激型:將所有數據繪製爲geom_point,並在ggplot2中包含顯示平均值的行;問題與stat_summary

WHAT I HAVE

我想是每況每刺激類型中的每個狀態的顏色加上平均線。理想情況下,這應該是這樣的:

WHAT I NEED

我一直在使用stat_summary和詳細的GGPLOT2文檔站點here 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

您可能需要生成摘要你開始避免分組問題之前。一種選擇是:

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() 

其中給出一個情節很像你表明: enter image description here

2

我使用dplyr計算了外部的平均值。平均值由正方形表示。你怎麼看待這件事?

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

相關問題