Code
library(ggplot2)
library(pwr)
library(patchwork)
library(ggplot2)
library(pwr)
library(patchwork)
# range of correlations
<- seq(.02, .1, .001)
r <- length(r)
nr
# range of Cohen's d
<- seq(.02, 0.2, length.out = nr) # Adjusted range for Cohen's d
d <- length(d)
nd
# power values
<- c(.80, .99)
p <- length(p) np
# obtain sample sizes for Pearson's correlation coefficient
<- array(numeric(nr*np), dim=c(nr,np))
samsize_r for (i in 1:np){
for (j in 1:nr){
<- pwr.r.test(n = NULL, r = r[j],
result sig.level = .05, power = p[i],
alternative = "two.sided")
<- ceiling(result$n)
samsize_r[j,i]
}
}
# obtain sample sizes for Cohen's d
<- array(numeric(nd*np), dim=c(nd,np))
samsize_d for (i in 1:np){
for (j in 1:nd){
<- pwr.t.test(n = NULL, d = d[j],
result sig.level = .05, power = p[i],
alternative = "two.sided")
<- ceiling(result$n)
samsize_d[j,i]
} }
# create dataframe for ggplot for Pearson's correlation coefficient
<- data.frame(r = rep(r, np), samsize = c(samsize_r[,1], samsize_r[,2]),
df_r power = rep(p, each = nr), measure = "Pearson's r")
# create dataframe for ggplot for Cohen's d
<- data.frame(d = rep(d, np), samsize = c(samsize_d[,1], samsize_d[,2]),
df_d power = rep(p, each = nd), measure = "Cohen's d")
# Rename columns in df_d to match df_r
names(df_d) <- c("r", "samsize", "power", "measure")
# Combine data frames
<- rbind(df_r, df_d) df_combined
# Define Hufflepuff colors
<- c("#FFDD00", "#000000")
hufflepuff_colors # Plot for Pearson's correlation coefficient
<- ggplot(df_r, aes(x = r, y = samsize, color = factor(power))) +
plot_r geom_line(linewidth = 1) +
scale_color_manual(values = hufflepuff_colors) +
labs(x = "Correlation Coefficient |r|",
y = "Sample Size (n)",
color = "Power",
title = "Power as a Function of Sample Size for r") +
theme_minimal() +
geom_hline(yintercept = 11865, linetype = "dashed", color = "black") +
scale_y_continuous(breaks = seq(0, 45000, by = 2500)) +
scale_x_continuous(breaks = seq(0, 0.1, by = 0.01)) + # Adjusted x-axis breaks
theme(legend.position = "none", text = element_text(size = 16)) # Adjust font size here
# Plot for Cohen's d
<- ggplot(df_d, aes(x = r, y = samsize, color = factor(power))) +
plot_d geom_line(linewidth = 1) +
scale_color_manual(values = hufflepuff_colors) +
labs(x = "Cohen's d",
y = "Sample Size (n)",
color = "Power",
title = "Power as a Function of Sample Size for Cohen's d") +
theme_minimal() +
geom_hline(yintercept = 11865, linetype = "dashed", color = "black") +
scale_y_continuous(breaks = seq(0, 100000, by = 5000)) +
scale_x_continuous(breaks = seq(0, .2, by = 0.05)) + # Adjusted x-axis breaks
theme(legend.position = "none", text = element_text(size = 16)) # Adjust font size here
# Combine plots using patchwork
<- plot_r + plot_d + plot_layout(ncol = 2)
combined_plot
# Add single legend at the bottom
<- combined_plot + theme(legend.position = "right", text = element_text(size = 16))
combined_plot
# Display the combined plot
print(combined_plot)
# Save the combined plot to a .tiff file with 600 dpi resolution
ggsave("combined_plot.tiff", plot = combined_plot, dpi = 600)
Saving 7 x 5 in image
# Save the combined plot to a .eps file
ggsave("combined_plot.eps", plot = combined_plot, device = "eps")
Saving 7 x 5 in image