我试图使用列作为分隔符在同一输出上创建多个条形图,但我只能创建单个图表。它现在作为一种功能,以便同时完成所有的事情。
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)
})发布于 2022-10-11 10:33:35
您可以尝试patchwork::wrap_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()

https://stackoverflow.com/questions/74026338
复制相似问题