3

I'd have a data set similar to the following that I would like to plot using facet_grid function:

IV1<-c('DO', 'PO', 'DO', 'PO', 'DO', 'PO', 'DO', 'PO', 'DO', 'PO', 'DO', 'PO',
       'DO', 'PO', 'DO', 'PO', 'DO', 'PO', 'DO', 'PO', 'DO', 'PO', 'DO', 'PO',
       'DO', 'PO', 'DO', 'PO', 'DO', 'PO', 'DO', 'PO', 'DO', 'PO', 'DO', 'PO',
       'DO', 'PO', 'DO', 'PO', 'DO', 'PO', 'DO', 'PO', 'DO', 'PO', 'DO', 'PO')
IV2<-c('DF', 'DF', 'SN', 'SN', 'SV', 'SV', 'DF', 'DF', 'SN', 'SN', 'SV', 'SV',
       'DF', 'DF', 'SN', 'SN', 'SV', 'SV', 'DF', 'DF', 'SN', 'SN', 'SV', 'SV',
       'DF', 'DF', 'SN', 'SN', 'SV', 'SV', 'DF', 'DF', 'SN', 'SN', 'SV', 'SV',
       'DF', 'DF', 'SN', 'SN', 'SV', 'SV', 'DF', 'DF', 'SN', 'SN', 'SV', 'SV')
IV3<-c('Adult', 'Adult', 'Adult', 'Adult', 'Adult', 'Adult', 'Child', 'Child', 'Child', 'Child', 'Child', 'Child', 
       'Adult', 'Adult', 'Adult', 'Adult', 'Adult', 'Adult', 'Child', 'Child', 'Child', 'Child', 'Child', 'Child',
       'Adult', 'Adult', 'Adult', 'Adult', 'Adult', 'Adult', 'Child', 'Child', 'Child', 'Child', 'Child', 'Child',
       'Adult', 'Adult', 'Adult', 'Adult', 'Adult', 'Adult', 'Child', 'Child', 'Child', 'Child', 'Child', 'Child')
Subj<-as.character(c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4,
                     5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8))
Value<-runif(48)
data<-data.frame(Subj, IV1, IV2, IV3, Value)

I can successfully plot the mean data like this:

library(ggplot2)
agg<-aggregate(Value~IV1*IV2*IV3, data=data, FUN="mean")

(P1<-ggplot(agg, aes(x=IV1, y=Value)) + theme_bw() + facet_grid(IV3~IV2) + 
  geom_point(aes(size=1.5, colour=factor(IV2), shape=factor(IV1))))

This looks like this: enter image description here

I can also successfully plot the individual subject data like so:

(P2<-ggplot(data, aes(x=IV1, y=Value, group=Subj)) + theme_bw() + 
facet_grid(IV3~IV2)+ 
geom_point(aes(group=Subj, colour=factor(IV2), shape=factor(IV1))))

Which looks like this: enter image description here

However, I would ideally like for (a) both the aggregate mean and the Subj data to be on the same plot (with the mean data points being larger) and (b) for the data points within each facet to be joined between the DO and PO points (for both the aggregate mean and the individual Subj data points).

A bit like this mock-up here (make in paint): enter image description here

Thanks in advance!

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
smh505
  • 53
  • 1
  • 5
  • 4
    Possible duplicate of [Combine Points with lines with ggplot2](https://stackoverflow.com/questions/8592585/combine-points-with-lines-with-ggplot2) – BLT Aug 02 '17 at 16:08
  • 1
    What's stopping you? Each `geom_point()` or `geom_line()` layer can take it's own data argument.... see the help pages. – Gregor Thomas Aug 02 '17 at 16:11

1 Answers1

4
agg$Subj <- rep(1:2, each = nrow(agg) / 2)
ggplot(data, aes(x=IV1, y=Value, colour=factor(IV2), shape=factor(IV1), group = Subj)) +  
    facet_grid(IV3~IV2)+ 
    geom_point() + 
    geom_point(data = agg, size = 5) +
    geom_line() +
    geom_line(data = agg, linetype = "dashed") +
    theme_bw()

enter image description here

I'm using another linetype to visually distinguish between actual observations and aggregate values (just an idea to consider).

Community
  • 1
  • 1
tonytonov
  • 25,060
  • 16
  • 82
  • 98
  • This is perfect - thank you! Out of interest why did do agg$Subj <- rep(1:2, each = nrow(agg) / 2) before the plot? Might be helpful to know for next time :) – smh505 Aug 02 '17 at 17:03
  • You're welcome! This is required since `agg` does not have `Subj` column like `data` does, but it still needs the `group` mapping, so that `ggplot` understands which points should be connected in `geom_line`. I'm just adding it manually for consistency (there are many ways to achieve the same result). – tonytonov Aug 02 '17 at 18:19