Reputation: 11
I'm trying to create multiple bar charts on the same output using columns as a separator, but I can only manage to create individual charts. It is now made as a function in order to do everything at once.
library("skimr")
library("tidyverse")
library("ggplot2")
V1 <- c(2,4,2,1,4)
V2 <- c(5,2,2,1,3)
V3 <- c(2,4,3,3,3)
V4 <- c(2,1,1,1,2)
df <- data.frame(V1,V2,V3,V4)
lapply(names(df), function(col) {
ggplot(df, aes(.data[[col]], ..count..), beside=TRUE) +
geom_bar() +
theme_minimal()+
coord_cartesian(xlim = c(0, 5), ylim = c(0, 5))+
scale_x_discrete(labels=c("1" = "Not relevant", "2" = "2","3" = "3","4" = "4", "5" = "Highly relevant"), na.value = FALSE)
})
Upvotes: 1
Views: 134
Reputation: 11878
You could try patchwork::wrap_plots()
to plot the list of plots:
library("ggplot2")
V1 <- c(2, 4, 2, 1, 4)
V2 <- c(5, 2, 2, 1, 3)
V3 <- c(2, 4, 3, 3, 3)
V4 <- c(2, 1, 1, 1, 2)
df <- data.frame(V1, V2, V3, V4)
lapply(names(df), function(col) {
ggplot(df, aes(.data[[col]], ..count..), beside = TRUE) +
geom_bar() +
theme_minimal() +
coord_cartesian(xlim = c(0, 5), ylim = c(0, 5)) +
scale_x_discrete(labels = c("1" = "Not relevant", "2" = "2", "3" = "3", "4" = "4", "5" = "Highly relevant"), na.value = FALSE)
}) |> patchwork::wrap_plots()
Upvotes: 1