1

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)
    })
1
  • See if this post can help. Commented Oct 11, 2022 at 10:14

1 Answer 1

1

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()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.