Skip to content

Instantly share code, notes, and snippets.

@kohske
Created April 30, 2012 14:04
Show Gist options
  • Save kohske/2558573 to your computer and use it in GitHub Desktop.
Save kohske/2558573 to your computer and use it in GitHub Desktop.
error bar for within-subject design
library(reshape2)
library(ggplot2)
set.seed(42)
v1 <- rnorm(20, 0, 20)
v2 <- v1 + 0.1 + rnorm(20, 0, 0.1)
df1 <- melt(data.frame(v1, v2))
TT1 <- t.test(value ~ variable, data = df1, paired = TRUE)
v1 <- rnorm(20, 0, 10)
v2 <- v1 + 2 + rnorm(20, 0, 10)
df2 <- melt(data.frame(v1, v2))
TT2 <- t.test(value ~ variable, data = df2, paired = TRUE)
df1$g <- 1
df2$g <- 2
df <- rbind(df1, df2)
ggplot(df, aes(variable, value)) +
stat_summary(fun.data = mean_se) +
facet_grid(.~g, labeller = function(x, y)
ifelse(y == "1", format(TT1$p.value), format(TT2$p.value)))
TT1
TT2
ggplot(df, aes(variable, value)) +
stat_summary(fun.data = mean_se) +
facet_grid(.~g, labeller = function(x, y)
ifelse(y == "1", format(TT1$p.value), format(TT2$p.value))) +
geom_point() + geom_line(aes(group = 1:20))
t.test(value ~ variable, data = df1, paired = FALSE)
t.test(value ~ variable, data = df2, paired = FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment