Skip to content

Instantly share code, notes, and snippets.

@dsquintana
Created November 12, 2016 19:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsquintana/c074684ee0a16a78565be611ec80bd8d to your computer and use it in GitHub Desktop.
Save dsquintana/c074684ee0a16a78565be611ec80bd8d to your computer and use it in GitHub Desktop.
# Load packages
library('cowplot')
library('ggplot2')
# Visualising small effect size
ES <- 0.25
mean1 <- ES*1 + 1
x <- seq(1 - 3*1, mean1 + 3*1, .01)
y1 <- dnorm(x, 1, 1)
df1 <- data.frame("x" = x, "y" = y1)
y2 <- dnorm(x, mean1, 1)
df2 <- data.frame("x" = x, "y" = y2)
y.poly <- pmin(y1,y2)
poly <- data.frame("x" = x, "y" = y.poly)
u3 <- 1 - pnorm(1, mean1,1)
u3 <- round(u3,3)
fig1a <- ggplot(df1, aes(x,y, color="treatment")) +
geom_line(size=1) +
geom_line(data=df2, aes(color="control"),size=1) +
geom_polygon(aes(color=NULL), data=poly, fill="red", alpha=I(4/10),
show.legend=FALSE) +
geom_vline(xintercept = 1, linetype="dotted") +
geom_vline(xintercept = mean1, linetype="dotted") +
scale_color_manual("Group",
values= c("treatment" = "black","control" = "red")) +
ylab(NULL) + xlab(NULL) +
scale_fill_discrete(name="Group",
breaks=c("control", "treatment"),
labels=c("Control group", "Case group")) +
theme(legend.position="none")
fig1a <- fig1a + theme(axis.text.x = element_text(size=24),
axis.text.y = element_text(size=24))
# Visualising medium effect size
ES <- 0.5
mean1 <- ES*1 + 1
x <- seq(1 - 3*1, mean1 + 3*1, .01)
y1 <- dnorm(x, 1, 1)
df1 <- data.frame("x" = x, "y" = y1)
y2 <- dnorm(x, mean1, 1)
df2 <- data.frame("x" = x, "y" = y2)
y.poly <- pmin(y1,y2)
poly <- data.frame("x" = x, "y" = y.poly)
u3 <- 1 - pnorm(1, mean1,1)
u3 <- round(u3,3)
fig1b <- ggplot(df1, aes(x,y, color="treatment")) +
geom_line(size=1) +
geom_line(data=df2, aes(color="control"),size=1) +
geom_polygon(aes(color=NULL), data=poly, fill="red", alpha=I(4/10),
show.legend=FALSE) +
geom_vline(xintercept = 1, linetype="dotted") +
geom_vline(xintercept = mean1, linetype="dotted") +
scale_color_manual("Group",
values= c("treatment" = "black","control" = "red")) +
ylab(NULL) + xlab(NULL) +
scale_fill_discrete(name="Group",
breaks=c("control", "treatment"),
labels=c("Control group", "Case group")) +
theme(legend.position="none")
fig2b <- fig2b + theme(axis.text.x = element_text(size=24),
axis.text.y = element_text(size=24))
# Visualising large effect size
ES <- 0.75
mean1 <- ES*1 + 1
x <- seq(1 - 3*1, mean1 + 3*1, .01)
y1 <- dnorm(x, 1, 1)
df1 <- data.frame("x" = x, "y" = y1)
y2 <- dnorm(x, mean1, 1)
df2 <- data.frame("x" = x, "y" = y2)
y.poly <- pmin(y1,y2)
poly <- data.frame("x" = x, "y" = y.poly)
u3 <- 1 - pnorm(1, mean1,1)
u3 <- round(u3,3)
fig1c <- ggplot(df1, aes(x,y, color="treatment")) +
geom_line(size=1) +
geom_line(data=df2, aes(color="control"),size=1) +
geom_polygon(aes(color=NULL), data=poly, fill="red", alpha=I(4/10),
show.legend=FALSE) +
geom_vline(xintercept = 1, linetype="dotted") +
geom_vline(xintercept = mean1, linetype="dotted") +
scale_color_manual("Group",
values= c("treatment" = "black","control" = "red")) +
ylab(NULL) + xlab(NULL) +
scale_fill_discrete(name="Group",
breaks=c("control", "treatment"),
labels=c("Control group", "Case group")) +
theme(legend.position="none")
fig1c <- fig1c + theme(axis.text.x = element_text(size=24),
axis.text.y = element_text(size=24))
fig1 <- plot_grid(fig1a, fig1b, fig1c, labels = c("A", "B", "C"), ncol = 3)
save_plot("fighist.jpg", fig1, base_height = 8, base_width = 20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment