Forked from JosiahParry/ggplots-in-for-loops-dont-work.R
Last active
May 4, 2026 18:38
-
-
Save jrosell/2acadb0b75777c565ec5a88d455c07a2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(ggplot2) | |
| # expected plot gg_manual | |
| manual_plots <- list() | |
| manual_plots[[1]] <- ggplot(penguins, aes(bill_len, m[,1])) + | |
| geom_point() + | |
| labs(title = "Column 1") | |
| manual_plots[[2]] <- ggplot(penguins, aes(bill_len, m[,2])) + | |
| geom_point() + | |
| labs(title = "Column 2") | |
| manual_plots[[3]] <- ggplot(penguins, aes(bill_len, m[,3])) + | |
| geom_point() + | |
| labs(title = "Column 3") | |
| manual_plots[[4]] <- ggplot(penguins, aes(bill_len, m[,4])) + | |
| geom_point() + | |
| labs(title = "Column 4") | |
| gg_manual <- patchwork::wrap_plots(manual_plots) | |
| gg_manual | |
| # The problem: This for loop does not plot data as expected | |
| all_plots <- list() | |
| m <- as.matrix(penguins[,3:6]) | |
| for (i in 1:ncol(m)) { | |
| gg <- ggplot(penguins, aes(bill_len, m[,i])) + | |
| geom_point() + | |
| labs(title = sprintf("Column %i", i)) | |
| all_plots[[i]] <- gg | |
| } | |
| gg_for <- patchwork::wrap_plots(all_plots) | |
| gg_for | gg_manual | |
| # Solution 1: Prepare colnames and use .data | |
| all_plots <- list() | |
| m <- as.matrix(penguins[,3:6]) | |
| m_cols <- colnames(penguins[,3:6]) | |
| for (i in 1:ncol(m)) { | |
| gg <- ggplot(m, aes(bill_len, .data[[m_cols[i]]])) + # show the column name as y axis text | |
| geom_point() + | |
| labs(title = sprintf("Column %i", i)) | |
| all_plots[[i]] <- gg | |
| } | |
| prepare_for <- patchwork::wrap_plots(all_plots) | |
| prepare_for | gg_manual | |
| # Solution 2: ggplotGrob to evaluate the plot (Source https://bsky.app/profile/tjmahr.com/post/3ml26koql7k2n) | |
| all_plots <- list() | |
| m <- as.matrix(penguins[, 3:6]) | |
| for (i in 1:ncol(m)) { | |
| gg <- ggplot(penguins, aes(bill_len, m[, i])) + # show m[, i] as y axis text | |
| geom_point() + | |
| labs(title = sprintf("Column %i", i)) | |
| all_plots[[i]] <- ggplotGrob(gg) | |
| } | |
| gggg_for <- patchwork::wrap_plots(all_plots) | |
| gggg_for | gg_manual | |
| # Solution 3: bang bang (Source: https://gist.github.com/JosiahParry/a013c715dc719abf075a0cd1193f5aed?permalink_comment_id=5808395#gistcomment-5808395) | |
| all_plots <- list() | |
| m <- as.matrix(penguins[,3:6]) | |
| m_cols <- colnames(penguins[,3:6]) | |
| for (i in 1:ncol(m)) { | |
| gg <- ggplot(penguins, aes(bill_len, m[, i])) + # show m[, 1], m[, 2]... as y axis text | |
| geom_point() + | |
| labs(title = sprintf("Column %i", i)) | |
| all_plots[[i]] <- gg | |
| } | |
| bangbang_for <- patchwork::wrap_plots(all_plots) | |
| bangbang_for | gg_manual |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment