2016-06-23 3 views
2

Ich zeichne gerne alle Datenpunkte mit Linien zwischen ihnen, die Teilnehmer bezeichnen. Hier habe ich jedes meiner Teilnehmer Bewertungen geplottet entsprechend dem Zustand und dem Stimulustyp:Plotten aller Daten als geom_point und einschließlich der Linien, die Mittel in ggplot2 zeigen; Probleme mit stat_summary

WHAT I HAVE

Was ich will, ist mittler Zeilen pro Stimulustyp pro Bedingung in der Farbe jeder Bedingung hinzuzufügen. Im Idealfall würde dies wie folgt aussehen:

WHAT I NEED

Ich habe versucht, stat_summary und stat_sum_df mit als here auf der ggplot2 Dokumentation Website detailliert, aber das kann ich nicht an die Arbeit. Es tut entweder nichts oder es zeichnet Linien für jeden einzelnen Teilnehmer.

Der Code, den ich das erste grafische Darstellung zu erzeugen verwendet wird, ist wie folgt:

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

... und Sie können ein Beispiel Datenrahmen für die ersten 4 Teilnehmer wie folgt erstellen:

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) 

I Ich denke, das Problem könnte mit meiner Gruppierungsvariablen ParticipantCondition auftreten, die ich erstellt habe, um die Linien zwischen Punkten für jeden Teilnehmer für jede Bedingung zu erhalten.

Jede Hilfe würde sehr geschätzt werden.

Antwort

2

Möglicherweise müssen Sie die Zusammenfassungen erzeugen, bevor Sie die Gruppierung Problem zu vermeiden starten. Eine Option ist:

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

die viel ein Grundstück gibt, wie Sie zeigte: enter image description here

2

Ich berechnete die Mittelwerte extern mit dplyr. Die Mittelwerte sind durch die Quadrate dargestellt. Was denkst du darüber?

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

Die Größe der Rechtecke kann natürlich leicht angepasst werden.

enter image description here

Verwandte Themen