微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

带有geom_col的R方面?

如何解决带有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))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。