1

Look I'm sure this has been asked before, but I've hit another wall, I would like to plot the average line over multiple lines on a plot. I can't seem to be able to do do it after many hours of attempts. I know it looks lazy and I'm super sorry but please help a brother out im at the end of my wits. I would just like the average lines plotted over all of the experimental lines. I've tried using rowMeans, and tried to generate new data frames by grouping my data with no success.please. https://www.dropbox.com/s/m1ao29xaudksanf/e1.txt?dl=0 https://www.dropbox.com/s/q0sf3hew2pco73s/c1.txt?dl=0

cc1L3 <- read.table('./Colony 1 Location 3/c1.txt', header=TRUE)
ec1L3 <- read.table('./Colony 1 Location 3/e1.txt', header=TRUE)
Col1Loc3 <- ggplot()+
geom_smooth(data=ec1L3, aes(Time,Current1, colour= 'experimental1L3'))+
geom_smooth(data=ec1L3, aes(Time,Current2, colour= 'experimental1L3'))+
geom_smooth(data=ec1L3, aes(Time,Current3, colour= 'experimental1L3'))+
geom_smooth(data=ec1L3, aes(Time,Current4, colour= 'experimental1L3'))+
geom_smooth(data=ec1L3, aes(Time,Current5, colour= 'experimental1L3'))+
geom_smooth(data=ec1L3, aes(Time,Current6, colour= 'experimental1L3'))+
geom_smooth(data=ec1L3, aes(Time,Current7, colour= 'experimental1L3'))+
geom_smooth(data=ec1L3, aes(Time,Current8, colour= 'experimental1L3'))+
geom_smooth(data=ec1L3, aes(Time,Current9, colour= 'experimental1L3'))+
geom_smooth(data=ec1L3, aes(Time,Current10, colour= 'experimental1L3'))+
###############
geom_smooth(data=cc1L3, aes(Time,Current1, colour='control1L3'))+
geom_smooth(data=cc1L3, aes(Time,Current2, colour='control1L3'))+
geom_smooth(data=cc1L3, aes(Time,Current3, colour='control1L3'))+
geom_smooth(data=cc1L3, aes(Time,Current4, colour='control1L3'))+
geom_smooth(data=cc1L3, aes(Time,Current5, colour='control1L3'))+
geom_smooth(data=cc1L3, aes(Time,Current6, colour='control1L3'))+
geom_smooth(data=cc1L3, aes(Time,Current7, colour='control1L3'))+
geom_smooth(data=cc1L3, aes(Time,Current8, colour='control1L3'))+
geom_smooth(data=cc1L3, aes(Time,Current9, colour='control1L3'))+
geom_smooth(data=cc1L3, aes(Time,Current10, colour='control1L3'))
Axeman
  • 32,068
  • 8
  • 81
  • 94

2 Answers2

1

I'm not entirely sure what you're after, but perhaps you want something like this:

cc1L3 <- read.table('https://www.dropbox.com/s/m1ao29xaudksanf/e1.txt?dl=1', header=TRUE)
ec1L3 <- read.table('https://www.dropbox.com/s/q0sf3hew2pco73s/c1.txt?dl=1', header=TRUE)

library(dplyr)

df <- bind_rows(
  control = gather(cc1L3, variable, value, -Time),
  experimental = gather(ec1L3, variable, value, -Time),
  .id = 'treatment'
)

ggplot(df, aes(Time, value, color = treatment)) +
  geom_smooth(aes(group = interaction(variable, treatment)), se = FALSE, size = 0.5) +
  geom_smooth(se = FALSE, size = 2)

Reshaping your data is crucial.

enter image description here

Axeman
  • 32,068
  • 8
  • 81
  • 94
0

What About calculating the mean() separately ?

ec1L3$mean <- apply(ec1L3[2:ncol(ec1L3)],1, mean) # Calculating by row the mean
cc1L3$mean <- apply(cc1L3[2:ncol(cc1L3)],1, mean) # Calculating by row the mean
## Add each line to each graphic 
geom_smooth(data=ec1L3, aes(Time,mean, colour= 'experimental1L3'))
geom_smooth(data=cc1L3, aes(Time,mean, colour= 'control1L3'))

I hope it helps. Cheers !

Carles
  • 2,731
  • 14
  • 25
  • 2
    Note that there is a `rowMeans` function! Also your apply call errors for me, `2:ncol(ec1L3)` is just a vector from 2 to 11.... – Axeman Jun 14 '18 at 11:50
  • Yes, because the first column of his dataset is the x line – Carles Jun 14 '18 at 15:16
  • 1
    But your not `apply`ing the function to the data, you are applying to `2:11`. Perhaps you were trying to do e.g. `apply(ec1L3[2:ncol(ec1L3)],1, mean)` – Axeman Jun 14 '18 at 15:18
  • You are right on that one. I did not see that mistake of mine. I have corrected it :) – Carles Jun 14 '18 at 15:21