Based on the example below, I want to create the same chart using the average values of "power" over hemisphere (left and right) and region (frontal and motor).
More specifically, I would like two columns in the chart: hemisphere (based on the average value over left and right) and region (based on the average value over frontal and motor).
I appreciate any suggestions!
library(ggplot2)
library(dplyr)
df <- expand.grid(
group = c("Children", "Teenagers"),
task = c("A", "B", "C"),
hemisphere = c("Left", "Right"),
region = c("Frontal", "Motor"),
frequency = c("Alpha", "Beta", "Theta")
)
df$power <- runif(nrow(df))
print(df)
interaction_plot <- df %>%
mutate(frequency = factor(frequency, levels = c("Alpha", "Beta", "Theta"))) %>%
ggplot(aes(x = task, y = power, color = group)) +
stat_summary(fun = mean, geom = "line", aes(group = group), size = 1.5) +
stat_summary(fun.data = mean_cl_boot, geom = "errorbar", width = 0.3, position = position_dodge(0.2)) +
facet_grid(frequency ~ hemisphere + region, space = "free_y", scales = "free_y") +
labs(title = "",
x = "Task", y = "Mean EEG Band Power",
color = "Group") +
theme_minimal(base_size = 12) +
theme(strip.background = element_blank(), # Optional: remove strip background for clarity
strip.text.y = element_text(angle = 0, hjust = 0.5, size = 14)) # Adjust text angle and size
interaction_plot
Not 100% sure about your desired result. If you want to create the same chart without differentiating by Group
, then drop color=Group
and use group = 1
in the stat_summary
which draws the line to connect the means:
library(ggplot2)
library(dplyr, warn = FALSE)
set.seed(123)
interaction_plot <- df %>%
mutate(frequency = factor(frequency, levels = c("Alpha", "Beta", "Theta"))) %>%
ggplot(aes(x = task, y = power)) +
stat_summary(
fun = mean, geom = "line", aes(group = 1), size = 1.5
) +
stat_summary(
fun.data = mean_cl_boot, geom = "errorbar",
width = 0.3, position = position_dodge(0.2)
) +
facet_grid(frequency ~ hemisphere + region, space = "free_y", scales = "free_y") +
labs(
title = "",
x = "Task", y = "Mean EEG Band Power",
color = "Group"
) +
theme_minimal(base_size = 12) +
theme(
strip.background = element_blank(), # Optional: remove strip background for clarity
strip.text.y = element_text(angle = 0, hjust = 0.5, size = 14)
) # Adjust text angle and size
interaction_plot