r - Create individual plots for each column -
i have dataframe similar df
below. each factor, i'd create boxplot comparing factor final score. without loop, looks code below. in practice, want 40 columns, , display in grid. sort of looping through columns seems appropriate here, i'm unsure how that.
library(ggplot2) library(gridextra) scores <- c(97, 98, 90, 92) factor1 <- c(1, 0, 0, 1) factor2 <- c(2, 1, 2, 0) factor3 <- c(0, 0, 0, 1) df <- data.frame(scores, factor1, factor2, factor3) plot1 <- ggplot(df, aes(x=factor(factor1), y=scores)) + geom_boxplot() plot2 <- ggplot(df, aes(x=factor(factor2), y=scores)) + geom_boxplot() plot3 <- ggplot(df, aes(x=factor(factor3), y=scores)) + geom_boxplot() grid.arrange(plot1, plot2, plot3, ncol=2)
library(ggplot2) library(gridextra) df <- data.frame(scores=c(97, 98, 90, 92), factor1=c(1, 0, 0, 1), factor2=c(2, 1, 2, 0), factor3=c(0, 0, 0, 1)) fun <- function(x) { dts <- df[,c("scores",x)] names(dts)[2] <- "varx" p <- ggplot(dts, aes(x=factor(varx), y=scores)) + geom_boxplot() + xlab(x) return(p) } ps <- lapply(names(df)[-1], fun) grid.arrange(grobs=ps, ncol=2)
Comments
Post a Comment