Using ggplot2 to create graphs in separate colors for each condition (time series data)

If it was answered somewhere, please point me in the right direction. I honestly tried, but did not find anything, regarding this particular problem:

I collected time series data from 20 people. In particular, the relative pupil size (y) over time (= x, 51 time bits) in response to 60 images / sound stimuli. There are two manipulations within the subject: half of the stimuli (= 30) are emotional, the remaining 30 are neutral. Half of the emotional and neutral stimuli were triggered during a previous sleep.

So we have 15 emotional and reactivated 15 emotional and non-reactive 15 neutral and reactivated and 15 neutral and non-reactive (2x2 valcode versus repcode). I have one column describing the valency (emotional or neutral) called "valcode" (1 = neutral, 2 = emotional) and one column defining the reactivation status of "repcode" (0 = non-reactive, 2 = reactivated).

Here are the first lines of data:

    head(df)
      time valcode  pupil_size repcode stimuli subject 
    1    0       2 0.000000000       0     1   1
    2  100       2 0.014501468       0     1   1
    3  200       2 0.004117636       0     1   1
    4  300       2 0.011774388       0     1   1
    5  400       2 0.061472275       0     1   1
    6  500       2 0.083575322       0     1   1

I would like to construct the data so that each of the four conditions has a different color. So far I have managed to get different shapes for repcode = 0 compared to repcode = 1 and different colors for valcode = 1 compared to 2 using the following line:

    dfPlot <- ggplot(df, aes(x=time, y=pupil_size, shape=repcode, colour=valcode))
    dfPlot + stat_summary(fun.y=mean, geom = "line", size=1) +stat_summary(fun.data=mean_se, geom="pointrange", size=1) + theme_bw(base_size = 10) + ggtitle("Pupil response") + ylab("Normalised pupil size") + xlab("Time relative to baseline (in ms)") 

I tried inserting color = c (repcode, valcode), but R gives me the following error message:

   Error: Aesthetics must either be length one, or the same length as the dataProblems:time, pupil_corr

, , - repcode valcode . ?

!

+4

Source: https://habr.com/ru/post/1598846/


All Articles