如何使用 ggplot2 包在 X 轴上绘制 2 个分类变量并将两个连续变量绘制为 "fill"?

How to plot 2 categorical variables on X-axis and two continuous variables as "fill" using ggplot2 package?

我有一个包含两个分类变量的数据集,即 YearCategory 以及两个连续变量 TotalSalesAverageCount

    Year    Category      TotalSales    AverageCount
1   2013    Beverages      102074.29    22190.06
2   2013    Condiments      55277.56    14173.73
3   2013    Confections     36415.75    12138.58
4   2013    Dairy Products  30337.39    24400.00
5   2013    Seafood         53019.98    27905.25
6   2014    Beverages       81338.06    35400.00
7   2014    Condiments      55948.82    19981.72
8   2014    Confections     44478.36    24710.00
9   2014    Dairy Products  84412.36    32466.00
10  2014    Seafood         65544.19    14565.37

在 MS Excel 中,我们可以愉快地获得相同 table 的数据透视图,其中 Year 和 Category 为轴,TotalSales 和 AverageCount 为 sigma 值。

如何使用 R 绘制如图所示的图形,其中分类变量在同一张图中显示为多层

P.S。我可以看到的一种选择是,通过将数据框拆分为两个单独的数据框(在我们的例子中,一个用于 2013 年,另一个用于 2014 年),并在一个图上绘制两个图形,排列成多行以获得相同的效果。但是有没有办法画成如上图呢?


上面使用的示例数据

dat <- structure(list(Year = c(2013L, 2013L, 2013L, 2013L, 2013L, 2014L, 
2014L, 2014L, 2014L, 2014L), Category = structure(c(1L, 2L, 3L, 
4L, 5L, 1L, 2L, 3L, 4L, 5L), .Label = c("Beverages", "Condiments", 
"Confections", "Dairy Products", "Seafood"), class = "factor"), 
    TotalSales = c(102074.29, 55277.56, 36415.75, 30337.39, 53019.98, 
    81338.06, 55948.82, 44478.36, 84412.36, 65544.19), AverageCount = c(22190.06, 
    14173.73, 12138.58, 24400, 27905.25, 35400, 19981.72, 24710, 
    32466, 14565.37)), .Names = c("Year", "Category", "TotalSales", 
"AverageCount"), class = "data.frame", row.names = c(NA, -10L
)

您需要首先重新格式化您的数据,因为@EDi 在您的一个较早的问题 () 中向您展示了如何进行格式化,@docendo discimus 在评论中提出了建议。

library(reshape2)
dat_l <- melt(dat, id.vars = c("Year", "Category"))

然后你可以像这样使用分面:

library(ggplot2)
p <- ggplot(data = dat_l, aes(x = Category, y = value, group = variable, fill = variable))
p <- p + geom_bar(stat = "identity", width = 0.5, position = "dodge")
p <- p + facet_grid(. ~ Year)
p <- p + theme_bw()
p <- p + theme(axis.text.x = element_text(angle = 90))
p

如果您特别有兴趣使图形更符合 Excel 外观,这里的答案中有一些策略可能会有所帮助:.

您的原始数据采用更易于粘贴的格式:

dat <- structure(list(Year = c(2013L, 2013L, 2013L, 2013L, 2013L, 2014L, 
2014L, 2014L, 2014L, 2014L), Category = structure(c(1L, 2L, 3L, 
4L, 5L, 1L, 2L, 3L, 4L, 5L), .Label = c("Beverages", "Condiments", 
"Confections", "Dairy Products", "Seafood"), class = "factor"), 
    TotalSales = c(102074.29, 55277.56, 36415.75, 30337.39, 53019.98, 
    81338.06, 55948.82, 44478.36, 84412.36, 65544.19), AverageCount = c(22190.06, 
    14173.73, 12138.58, 24400, 27905.25, 35400, 19981.72, 24710, 
    32466, 14565.37)), .Names = c("Year", "Category", "TotalSales", 
"AverageCount"), class = "data.frame", row.names = c(NA, -10L
))