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

I will have a dataset similar to the following that I would like to plot using the 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 average 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))))

      

It looks like this: enter image description here

I can also successfully plot 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 (a) both the cumulative mean and the Subj data to be on the same plot (with large data points) and (b) so that the data points inside each face are connected between DO and PO points (for both the cumulative average and the individual Subj data points).

A bit like this layout here (make paint): enter image description here

Thanks in advance!

+3


source to share


1 answer


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 use another one linetype

to visually distinguish between actual observations and cumulative values ​​(just a thought to consider).

+4


source







All Articles