r - 如何绘制具有数据框的分组 geom_bar 图?
解决方案
我们可以barplot
使用base R
barplot(t(df1), beside = TRUE, col = c("blue", "orange", "grey",
"yellow", "lightblue" ))
-输出
或者如果我们需要一个ggplot/plotly
library(ggplot2)
library(dplyr)
library(tidyr)
library(plotly)
library(tibble)
p <- df1 %>%
rownames_to_column('rn') %>%
pivot_longer(cols = -rn) %>%
ggplot(aes(x = rn, y = value, fill = name)) +
geom_col(position = 'dodge') +
theme_bw()
ggplotly(p)
-输出
数据
df1 <- structure(list(A = c(65, 9, 7, 70, 9), B = c(23, 4, 5, 53, 2),
C = c(42, 5, 2, 17, 7), D = c(51, 7, 5, 57, 5), E = c(14,
2, 2, 13, 4)), class = "data.frame", row.names = c("AAA",
"BBB", "CCC", "DDD", "EEE"))
推荐阅读
- android - Android/Kotlin - 如何循环所有 ToggleButtons 以创建 if/else 条件
- javascript - 如何在请求结果中使用变量来遍历数据?
- html - 使用引导程序 btn-group-toggle 在烧瓶中检索复选框按钮值的问题
- python - 有没有办法将多个字符串添加到 Text 对象?(泽尔图形)
- terraform - Terraform 资源使用模块生成多个 key_pair
- docker - DreamFactory 如何在 docker 容器中禁用包装器“资源”
- spring - spring boot打包后找不到资源文件
- php - How can I scroll while I increase the size of a div with Resizable Jquery?
- scripting - 在 After Effects 中通过脚本添加效果
- c# - 如何在 FaceRecognition 项目的非托管代码中添加方法