Combining geom_point and geom_line when using facet_grid and multiple data points (R ggplot2)

Viewed 2397

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!

1 Answers
Related