我看了一下类似的问题,但找不到我的答案和我的代码无法工作的原因,我有一个数据帧
# Number of participants, sessions, and session epochs
n_participants <- 5
n_sessions <- 7
n_session_epochs <- 4
# Create the initial data frame
participant_ids <- paste0("ID-", 1:n_participants)
sessions <- 1:n_sessions
session_epochs <- 1:n_session_epochs
# Create a data frame with all combinations of participants, sessions, and session epochs
df <- expand.grid(Participant = participant_ids,
Session = sessions,
Session_Epoch = session_epochs)
# Add F_response with random values
set.seed(123) # For reproducible results
df$F_response <- sample(1:4, nrow(df), replace = TRUE)
# Function to generate increasing accuracy values
generate_accuracy <- function() {
start_value <- runif(1, 0, 0.25)
end_value <- 1
seq(from = start_value, to = end_value, length.out = n_sessions * n_session_epochs)
}
# Apply the function to each participant and reshape the data
accuracy_values <- replicate(n_participants, generate_accuracy())
df$Accuracy <- as.vector(t(accuracy_values))
# Arrange the data frame by Participant, Session, and Session_Epoch
df <- df %>% arrange(Participant, Session, Session_Epoch)
现在我想创建一个曲线图,其中我只 for each 参与者创建了一个曲线图,其中我只连接了会话中的第一个Session_Epoch,我try 创建一个新的组,但似乎不起作用(我try 的代码如下所示),不确定原因
(a = df %>%
mutate(grp = if_else(Session_Epoch == 1, Session, NA)) %>%
ggplot(aes(x = Accuracy, y = F_response)) +
geom_point(aes(color = as.factor(Session), shape = as.factor(Session_Epoch)), size=3) +
geom_hline(yintercept = 0,linetype = "dashed", color = "grey") +
geom_path(aes(group = as.factor(grp))) +
theme_bw() +
facet_grid(~Participant) +
theme(text = element_text(size = 15)))