GFL
GFL

Reputation: 75

Plotting all data as geom_point and including lines showing means in ggplot2; issues with stat_summary

I like plotting all data points with lines between them denoting participants. Here, I have each of my participants' ratings plotted according to the condition and the stimulus type:

WHAT I HAVE

What I want is to add mean lines per condition per stimulus type in the colour of each condition. Ideally, this would look like this:

WHAT I NEED

I have tried using stat_summary and stat_sum_df as detailed on the ggplot2 documentation site here, but I can't get that to work. It either does nothing or it plots lines for every single participant.

The code I used to generate the first graph is as follows:

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

...and you can create an example dataframe for the first 4 participants as follows:

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 think the problem might be with my grouping variable ParticipantCondition which I created in order to get the lines between points for each participant for each condition.

Any help would be greatly appreciated.

Upvotes: 2

Views: 2138

Answers (3)

Jonas van Nijnatten
Jonas van Nijnatten

Reputation: 35

Here is a solution for which you don't need to summarize/aggregate the data first. Instead you can use your original data set and easily add individual data points if you want. The averages are calculated using ggplot's stat_summary options.

ggplot(df, aes(x=StimulusType, y = Rating, group=Condition, color=Condition)) + 
      # add individual lines + data points
      geom_line (aes(group=interaction(Condition,Participant)), linetype = "dashed", size=.5) +
      geom_point(size=.5) +
      # add mean lines + datapoints

      geom_line (stat="summary", fun.y="mean", size=1) +
      geom_point(stat="summary", fun.y="mean", size=2) +
      scale_colour_manual(values=c("#0072B2",  "#009E73", "#F0E442", "#D55E00"))

Upvotes: 0

Mark Peterson
Mark Peterson

Reputation: 9570

You may need to generate the summaries before you start to avoid the grouping issue. One option is:

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

Which gives a plot much like you showed: enter image description here

Upvotes: 2

Alex
Alex

Reputation: 4995

I calculated the mean values external by using dplyr. The mean values are represented by the squares. What do you think about this?

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

The size of the rectangles can of course easily be adjusted.

enter image description here

Upvotes: 2

Related Questions