geom_col 的 R 面?

R facet with geom_col?

我已将以下 excel 文件导入 RStudio

我的目标是显示 4 个不同的条形图,其中 x 轴表示算法的分类变量,y 每次显示百分比,因为它们已经在 Question_1、2、3 列中计算,4. 我使用这个简单的 R 代码只绘制了一个条形图

ggplot(data = R_Analysis_draft, mapping = aes(x = algorithms,y = Question_1)) +
     geom_col()

我如何在同一个方面显示所有 4 个,因为它们都有相同的分类变量 algorithms 在每个不同的问题之上放置一个 header 并使用百分比?

您可以使用

library(tidyverse)

df %>% 
  pivot_longer(-algorithms) %>% 
  ggplot(aes(x = algorithms, y = value, fill = name)) +
  geom_col()

或者

df %>% 
  pivot_longer(-algorithms) %>% 
  ggplot(aes(x = algorithms, y = value, fill = name)) +
  geom_col(position = position_dodge())

多面绘图使用

df %>% 
  pivot_longer(-algorithms) %>% 
  ggplot(aes(x = algorithms, y = value)) +
  geom_col(position = position_dodge()) + 
  facet_wrap(name~.)

数据

df = structure(list(algorithms = c("Alg_1", "Alg_2", "Alg_3", "Alg_4", 
"Alg_5"), Question_1 = c(51L, 43L, 48L, 48L, 54L), Question_2 = c(49L, 
35L, 53L, 39L, 63L), Question_3 = c(55L, 42L, 54L, 48L, 47L), 
    Question_4 = c(52L, 36L, 46L, 48L, 55L)), class = "data.frame", row.names = c(NA, 
-5L))