如何在X轴和两个连续变量上绘制2个分类变量作为“填充”使用ggplot2包? [英] How to plot 2 categorical variables on X-axis and two continuous variables as "fill" using ggplot2 package?

查看:157
本文介绍了如何在X轴和两个连续变量上绘制2个分类变量作为“填充”使用ggplot2包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,它有两个分类变量,即 Year Category 和两个连续变量 TotalSales AverageCount

 年份类别总计销售额AverageCount 
1 2013年饮料102074.29 22190.06
2 2013年调味品55277.56 14173.73
3 2013 Confections 36415.75 12138.58
4 2013乳制品30337.39 24400.00
5 2013海鲜53019.98 27905.25
6 2014饮料81338.06 35400.00
7 2014调味品55948.82 19981.72
8 2014 Confections 44478.36 24710.00
9 2014乳制品84412.36 32466.00
10 2014海鲜65544.19 14565.37

MS Excel中,我们可以愉快地获得同一个表的轴心图,Year和Category为AXIS,TotalSales和AverageCount为西格玛值。



使用R ,如何在图像中绘制这样的图形,在同一图形中分类变量显示为多个图层





我可以看到的一个选择是,通过将数据框分成两个独立的数据框(2013年的一个和我们的2014年的另一个),并在单个绘图上绘制两个图形,排列成多行以获得相同的效果。但有没有办法像上面那样绘制它?






上面使用的示例数据
(2013年,2013年,2013年,2013年,2013年,2014年,
2014年,2014年,2014年, 2014L),类别=结构(c(1L,2L,3L,
4L,5L,1L,2L,3L,4L,5L),。标签= c(Beverages,Condiments,$ b $ (102074.29,55277.56,36415.75,30337.39,53019.98,$ b $ 81338.06,55948.82,44478.36,bConfections,Dairy Products,Seafood),class =factor平均计数= c(22190.06,
14173.73,12138.58,24400,27905.25,35400,19981.72,24710,
32466,14565.37)),.Name = c(Year,Category ,TotalSales,
AverageCount),class =data.frame,row.names = c(NA,-10L


解决方案

您需要先对数据进行重新格式设置,因为@EDi向您展示了如何解决您的一个老问题(< a href =https:// stackoverflow / ggplot-multi-variable-multiple-continuous-variable-plotting> ggplot:多变量(多连续变量)绘图)和@docendo discimus在评论中提出。

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

然后你就可以这样使用faceting:

  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(。 〜年)
p< - p + theme_bw()
p< - p +主题(axis.text.x =元素文字(角度= 90))
p

如何绘制嵌套类别轴的图表

您的原始数据更容易粘贴:

  2014L,2014L,2014L,2014L),类别=结构(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,552 (22190.06,
14173.73,12138.58,24400,27905.25,35400,19981.72,24710,$ b $ (例如b 32466,14565.37)),.Names = c(Year,Category,TotalSales,
AverageCount),class =data.frame,row.names = c(NA, - 10L
))


I have a dataset that has two categorical variables, viz., Year and Category and two continuous variables TotalSales and AverageCount.

    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.

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?

P.S. 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. 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.

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?.

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

这篇关于如何在X轴和两个连续变量上绘制2个分类变量作为“填充”使用ggplot2包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆