I have a listed dataframe with the code:
df1 <- data.frame(ID = rep(c("P-1000", "P-10001", "P-10002", "P-1003", "P-1004"),3),
Visit = c(rep("M1",5), rep("M4",5), rep("M17",5)),
Value = runif(15))
df2 <- data.frame(ID = rep(c("P-1000", "P-10001", "P-10002", "P-1003", "P-1004"),3),
Visit = c(rep("M1",5), rep("M4",5), rep("M17",5)),
Value = runif(15))
df3 <- data.frame(ID = rep(c("P-1000", "P-10001", "P-10002", "P-1003", "P-1004"),3),
Visit = c(rep("M1",5), rep("M4",5), rep("M17",5)),
Value = runif(15))
sampledata<- list(df1, df2, df3)
names(sampledata)<- c("A", "C", "Z")
for which I am trying to make plots for. I am trying to make plots per ID per dataframe (e.g., 1 plot would be for P-1000 in dataframe A). In the end, I would like to save these plots in separate pdfs by dataframes (e.g., 1 pdf for all plots pertaining to dataframe A, another for dataframe B, etc). I'm not quite sure how to do this. My current trial of code is the following:
sample_data_plots <- lapply(seq_along(sampledata),function(i) {
sampledata[[i]] %>% filter(!is.na(Visit)) %>% group_by(ID) %>%
mutate(Visit = factor(Visit, levels = c("M1", "M4", "M17"))) %>%
ggplot(aes(x = Visit, y = Value)) + geom_boxplot() +
labs(title = paste("Values of", names(sampledata)[i], "for", "ID",
#this is the variable ID
),
y = "Value",
x = "Visit")})
I have no issues in running:
lapply(seq_along(sampledata), function(i) {
sampledata[[i]] %>% filter(!is.na(Visit)) %>%
mutate(Visit = factor(Visit, levels = c "M1", "M4", "M17"))) %>%
ggplot(aes(x = Visit, y = Value)) + geom_boxplot() +
labs(title = paste("Value", names(sampledata)[i]),
y = "Value",
x = "Visit") })
but this gives graphs per dataframes for all ID
Any help would be greatly appreciated. Thank you!