r - overlay mean and sem to ggplot per facet -
i have horrible line plot in use facet in order divide between groups (trained , untrained) , trials (e1, e2). tried how tendency used stat_smooth()
it's not hoped get. notice how bad red smooth lines represent data (specially in zones delimited vertical lines).
i'm thinking rather prefer plot mean , standar error in same way. can't figure how it. have looked on other posts have found information simple dotplots of 1 variable without replicates. plot comes 20 replicate each of 2 groups sampling rate of .2 sec it's more complicated.
¿should find way calculate mean , standar error each group , pass stat_smooth()
function?
unfortunately data bigger character limit provide toy example.
dput(toy) structure(list(tiempo = c(618.2, 618.4, 618.6, 618.8, 619, 619.2, 619.4, 619.6, 619.8, 620, 620.2, 620.4, 620.6, 620.8, 621, 621.2, 621.4, 621.6, 621.8, 622, 622.2, 622.4, 622.6, 622.8, 623, 623.2, 623.4, 623.6, 623.8, 624, 624.2, 624.4, 624.6, 624.8, 625, 625.2, 625.4, 625.6, 625.8, 626, 626.2, 626.4, 626.6, 626.8, 627, 3138.2, 3138.4, 3138.6, 3138.8, 3139, 3139.2, 3139.4, 3139.6, 3139.8, 3140, 3140.2, 3140.4, 3140.6, 3140.8, 3141, 3141.2, 3141.4, 3141.6, 3141.8, 3142, 3142.2, 3142.4, 3142.6, 3142.8, 3143, 3143.2, 3143.4, 3143.6, 3143.8, 3144, 3144.2, 3144.4, 3144.6, 3144.8, 3145, 3145.2, 3145.4, 3145.6, 3145.8, 3146, 3146.2, 3146.4, 3146.6, 3146.8, 3147), ut2x45.19 = c(8325l, 8324l, 8323l, 8321l, 8320l, 8317l, 8315l, 8313l, 8311l, 8307l, 8305l, 8302l, 8301l, 8298l, 8295l, 8294l, 8293l, 8292l, 8290l, 8287l, 8285l, 8283l, 8282l, 8281l, 8279l, 8275l, 8271l, 8270l, 8266l, 8263l, 8259l, 8257l, 8255l, 8254l, 8253l, 8252l, 8250l, 8246l, 8239l, 8235l, 8235l, 8234l, 8232l, 8229l, 8226l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l, 1579l), ut2x45.20 = c(17444l, 17445l, 17447l, 17448l, 17449l, 17449l, 17451l, 17451l, 17452l, 17451l, 17451l, 17451l, 17451l, 17452l, 17453l, 17459l, 17461l, 17463l, 17462l, 17462l, 17463l, 17466l, 17467l, 17467l, 17467l, 17466l, 17467l, 17467l, 17468l, 17469l, 17469l, 17470l, 17471l, 17473l, 17475l, 17475l, 17475l, 17475l, 17476l, 17477l, 17479l, 17481l, 17482l, 17482l, 17483l, 19867l, 19868l, 19869l, 19869l, 19869l, 19869l, 19869l, 19869l, 19869l, 19869l, 19869l, 19869l, 19869l, 19869l, 19870l, 19870l, 19870l, 19870l, 19870l, 19870l, 19871l, 19871l, 19872l, 19873l, 19875l, 19877l, 19878l, 19878l, 19878l, 19878l, 19879l, 19879l, 19879l, 19881l, 19882l, 19883l, 19884l, 19884l, 19887l, 19887l, 19888l, 19890l, 19892l, 19892l, 19892l), tr2x45.19 = c(15870l, 15870l, 15866l, 15860l, 15861l, 15863l, 15863l, 15872l, 15877l, 15857l, 15845l, 15842l, 15840l, 15830l, 15826l, 15824l, 15818l, 15815l, 15813l, 15810l, 15808l, 15804l, 15799l, 15798l, 15776l, 15746l, 15734l, 15727l, 15723l, 15723l, 15709l, 15671l, 15634l, 15617l, 15598l, 15582l, 15568l, 15557l, 15547l, 15537l, 15526l, 15517l, 15506l, 15501l, 15496l, 14338l, 14338l, 14338l, 14338l, 14338l, 14338l, 14338l, 14338l, 14337l, 14329l, 14324l, 14323l, 14322l, 14322l, 14322l, 14321l, 14321l, 14321l, 14321l, 14321l, 14321l, 14321l, 14321l, 14321l, 14321l, 14313l, 14309l, 14307l, 14306l, 14307l, 14304l, 14280l, 14253l, 14239l, 14234l, 14225l, 14218l, 14204l, 14193l, 14183l, 14174l, 14163l, 14153l, 14143l, 14138l), tr2x45.20 = c(15760l, 15760l, 15761l, 15768l, 15780l, 15781l, 15787l, 15795l, 15795l, 15791l, 15791l, 15791l, 15789l, 15790l, 15792l, 15795l, 15794l, 15794l, 15793l, 15793l, 15791l, 15791l, 15791l, 15793l, 15795l, 15798l, 15806l, 15809l, 15812l, 15818l, 15822l, 15825l, 15829l, 15833l, 15837l, 15838l, 15839l, 15840l, 15841l, 15843l, 15845l, 15847l, 15849l, 15851l, 15854l, 14945l, 14945l, 14945l, 14944l, 14942l, 14943l, 14943l, 14943l, 14943l, 14942l, 14942l, 14942l, 14942l, 14942l, 14942l, 14942l, 14942l, 14942l, 14942l, 14942l, 14942l, 14942l, 14942l, 14942l, 14942l, 14941l, 14939l, 14939l, 14939l, 14939l, 14939l, 14939l, 14938l, 14938l, 14937l, 14936l, 14936l, 14936l, 14936l, 14936l, 14936l, 14936l, 14935l, 14935l, 14935l), ensayo = structure(c(1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l), .label = c("e1", "e2"), class = "factor")), .names = c("tiempo", "ut2x45.19", "ut2x45.20", "tr2x45.19", "tr2x45.20", "ensayo"), row.names = c("3088", "3089", "3090", "3091", "3092", "3093", "3094", "3095", "3096", "3097", "3098", "3099", "3100", "3101", "3102", "3103", "3104", "3105", "3106", "3107", "3108", "3109", "3110", "3111", "3112", "3113", "3114", "3115", "3116", "3117", "3118", "3119", "3120", "3121", "3122", "3123", "3124", "3125", "3126", "3127", "3128", "3129", "3130", "3131", "3132", "15688", "15689", "15690", "15691", "15692", "15693", "15694", "15695", "15696", "15697", "15698", "15699", "15700", "15701", "15702", "15703", "15704", "15705", "15706", "15707", "15708", "15709", "15710", "15711", "15712", "15713", "15714", "15715", "15716", "15717", "15718", "15719", "15720", "15721", "15722", "15723", "15724", "15725", "15726", "15727", "15728", "15729", "15730", "15731", "15732"), class = "data.frame")
and functions
deriv_abs<- function(tiempo,dataframe){ matriz = as.matrix(dataframe) matriz_dif = abs(diff(matriz ))# derivar matriz_dif1= rbind(c(rep(0,dim(matriz_dif)[2])),matriz_dif) #agrega linea de ceros para compensar diff return(data.frame(cbind(tiempo, matriz_dif1))) } vel_us<-by( toy[,(2:length(toy))-1], toy$ensayo, fun= function(x) deriv_abs(x$tiempo, x[,-1] )) names(vel_us$e1)<-names(toy[2:length(toy)-1]) names(vel_us$e2)<-names(toy[2:length(toy)-1]) df4<-data.frame(rbind(vel_us$e1,vel_us$e2),ensayo=us$ensayo) m_absvel_us<-melt(df4,id=c("ensayo","tiempo")) m_absvel_us$trat<-rep(na,length(m_absvel_us)) m_absvel_us$trat[grep("tr",m_absvel_us$variable)]<-c("tr") m_absvel_us$trat[grep("ut",m_absvel_us$variable)]<-c("ut") us_absvel_plot<-ggplot(m_absvel_us,aes(x=tiempo,y=value,fill=trat,color=trat))+theme_minimal() us_absvel_plot+geom_line(aes(fill=variable))+facet_wrap(trat~ensayo ,scales="free_x")+labs(title=c("velocidad por ensayo entrenamiento")) + geom_vline(aes(xintercept = tiempo),lty=2, vline.data) +stat_smooth(aes(fill=trat))
for more detail, add plot first row of facets (mean of data). that's want overlaid (or maybe smooth version sem included)
to add mean line , standard error of mean:
us_absvel_plot + stat_summary(fun.y=mean, geom="line", colour="red") + stat_summary(fun.data=mean_cl_normal, geom="errorbar", confint=0.683, colour="red") + stat_summary(fun.y=mean, geom="point", colour="blue")
setting conf.int
0.683 gives error bar equal +/- 1 standard error.
to smoother follows data more closely, there number of options. example, if use "loess" smoother (the default data less 1000 observations per group), can this:
geom_smooth(method="loess", span=1, se=false, colour="red", size=2)
you'll have play value of span
. lower span result in more flexible smoothing function.
you try natural splines. in case, set higher degrees of freedom, df
, more flexible function:
library(splines) geom_smooth(method="lm", formula=y ~ ns(x,df=10), colour="red", size=2)
Comments
Post a Comment