简体   繁体   English

如何使用ggplot2包将X轴上的2个分类变量和两个连续变量绘制为“填充”?

[英]How to plot 2 categorical variables on X-axis and two continuous variables as “fill” using ggplot2 package?

I have a dataset that has two categorical variables, viz., Year and Category and two continuous variables TotalSales and AverageCount . 我有一个数据集,它有两个分类变量,即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

In MS Excel, we can happily get a pivot-plot for the same table, with Year and Category as AXIS, TotalSales and AverageCount as sigma values. 在MS Excel中,我们可以愉快地获得同一个表的数据透视图,其中Year和Category为AXIS,TotalSales和AverageCount为sigma值。

Using R, how do I draw such a graph as shown in the image, where the categorical variables are shown as multiple layers in the same graph? 使用R,如何绘制如图所示的图形, 其中分类变量在同一图表中显示为多个图层

在此输入图像描述

PS One option that I could see is, by splitting the data frame into two separate dataframes (One for year 2013 and another for year 2014 in our case) and draw two graphs on one single plot, arranged in multiple rows to get the same effect. PS我可以看到的一个选项是,将数据框分成两个独立的数据框(一个是2013年,另一个是2014年的另一个),并在一个图上绘制两个图,排成多行以获得相同的效果。 But is there any way to draw it as shown above? 但有没有办法如上所示绘制它?


Sample data used above 上面使用的示例数据

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
)

You need to first reformat your data, as @EDi showed you how to in one of your older questions ( ggplot : Multi variable (multiple continuous variable) plotting ) and @docendo discimus suggested in the comments. 您需要首先重新格式化数据,因为@EDi向您展示了如何在一个较旧的问题( ggplot:多变量(多个连续变量)绘图 )和评论中建议的@docendo discimus。

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

Then you can use faceting like so: 然后你可以像这样使用刻面:

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

在此输入图像描述

If you are particularly interested in making the figure more consistent with an Excel-look, there are some strategies in the answer here that might be helpful: How do I plot charts with nested categories axes? 如果您对使图形更符合Excel外观特别感兴趣,那么答案中的一些策略可能会有所帮助: 如何使用嵌套类别轴绘制图表? .

Your original data in an easier to paste format: 您的原始数据采用更易于粘贴的格式:

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
))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改 ggplot2 中分类变量的 x 轴大小? - How to change the size of x-axis for categorical variables in ggplot2? 如何从 ggplot2 中的两个分类变量创建 x 轴标签? - How do I create x-axis labels from two categorical variables in ggplot2? 如何使用ggplot2 boxplot绘制多个变量与单个x轴的关系图 - How to plot multiple variables vs single x-axis using ggplot2 boxplot 如何 Plot 一个在 X 轴上有两个分类变量的条形图(一个由另一个嵌套,但都可见),以及其他变量作为填充? - How to Plot a Bar Graph with Two Categorical Variables on X-Axis (one nested by the other, but both visible), and other variables as fill? 如何在 ggplot2 中重新排序 x 轴上的分类变量? - How to reorder categorical variables in x axis in ggplot2? 如何在R中的x轴上绘制带有分类变量的散点图 - How to plot a scatterplot with categorical variables in the x-axis in R ggplot2条形图,带有两个分类变量 - ggplot2 bar plot with two categorical variables 如何在R轴的x轴上制作带有两个分类变量的散点图 - How to make scatterplot with two categorical variables on x-axis in R 如何在 ggplot2 中绘制具有正确间隔的连续 x 轴值的箱线图 - How to plot a boxplot with correctly spaced continuous x-axis values in ggplot2 如何使用连续的x轴(ggplot2)制作点图? - How do I make a dot plot with a continuous x-axis (ggplot2)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM