带有geom_col的R方面?

问题描述

我已将以下 excel 文件导入 RStudio

enter image description here

我的目标是在 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 在每个不同的问题顶部放置一个标题并使用百分比?

解决方法

你可以使用

library(tidyverse)

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

enter image description here

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

enter image description here

用于分面绘图

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

enter image description here 数据

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