制作分组条形图

Making a grouped bar plot

我正在尝试预测葡萄酒质量,我有如下数据。

               columns         white_wine           red_wine
1        fixed_acidity    6.8547876684361   8.31963727329581
2     volatile_acidity   0.27824111882401  0.527820512820513
3          citric_acid  0.334191506737444  0.270975609756098
4       residual_sugar   6.39141486320947   2.53880550343965
5            chlorides 0.0457723560636995 0.0874665415884928
6   free_sulfurdioxide   35.3080849326256   15.8755472170106
7  total_sulfurdioxide   138.360657411188   46.4684177611007
8              density  0.994027376480196  0.996746679174484
9                   pH   3.18826663944467   3.31111319574734
10           sulphates  0.489846876276031  0.658148843026892
11             alcohol   10.5142670477701   10.4229831144465
12            red_wine   5.87790935075541   5.63602251407129
13          white_wine    6.8547876684361   8.31963727329581
14             quality   0.27824111882401  0.527820512820513

我正在尝试绘制一个分组条形图,显示列行中的每个值及其各自的白色平均值和 red_wine 组合在一起的值。像下面这样的东西。有谁知道如何在 R 中实现它?

我们可以使用 pivot_longer 重塑为 'long' 格式并使用 ggplot

绘图
library(dplyr)
library(tidyr)
library(ggplot2)
df1 %>%
    pivot_longer(cols = -columns) %>%
    ggplot(aes(x = columns, y = value, fill = name)) + 
    geom_col()+
    theme(axis.text.x = element_text(angle = 70, hjust = 1))

-输出


如果我们需要dodge

df1 %>%
     pivot_longer(cols = -columns) %>%
     ggplot(aes(x = columns, y = value, fill = name)) + 
     geom_col(position = 'dodge')+
     theme(axis.text.x = element_text(angle = 70, hjust = 1))+
     ggtitle("Mean values of features for red and white wine")

-输出

数据

df1 <- structure(list(columns = c("fixed_acidity", "volatile_acidity", 
"citric_acid", "residual_sugar", "chlorides", "free_sulfurdioxide", 
"total_sulfurdioxide", "density", "pH", "sulphates", "alcohol", 
"red_wine", "white_wine", "quality"), white_wine = c(6.8547876684361, 
0.27824111882401, 0.334191506737444, 6.39141486320947, 0.0457723560636995, 
35.3080849326256, 138.360657411188, 0.994027376480196, 3.18826663944467, 
0.489846876276031, 10.5142670477701, 5.87790935075541, 6.8547876684361, 
0.27824111882401), red_wine = c(8.31963727329581, 0.527820512820513, 
0.270975609756098, 2.53880550343965, 0.0874665415884928, 15.8755472170106, 
46.4684177611007, 0.996746679174484, 3.31111319574734, 0.658148843026892, 
10.4229831144465, 5.63602251407129, 8.31963727329581, 0.527820512820513
)), class = "data.frame", row.names = c("1", "2", "3", "4", "5", 
"6", "7", "8", "9", "10", "11", "12", "13", "14"))