R堆积条形图与汇总数据 [英] R stacked barchart with aggregate data

查看:382
本文介绍了R堆积条形图与汇总数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了用集合数据创建堆叠条形图的麻烦。在处理来自其他人的报告的聚合表时,我通常使用Excel,但是我想开始在R中完成所有的图表,可能使用格或ggplot。在Excel中,对下列汇总数据进行堆叠的条形图需要几次点击(插入,柱形图,堆积列),并且您得到了类似这样的内容。。

I'm having troubles creating a stacked barchart with aggregate data. When dealing with aggregate tables from other people's reports I generally use Excel, but I'd like to start doing all my charts in R, possibly with lattice or ggplot. In Excel doing a stacked barchart of the following aggregate data takes a couple of clicks (Insert, Column Charts, Stacked Column), and you get something like this..

除了想在R中使用这个图表,我还希望使用ggplot的faceting,在ggplot中并排放置两个堆叠的barcharts,以比较两个组(A和B)。我与其他图表一起玩过,这似乎是最佳选择。这是数据.Excel图表只显示组A(数字是百分比)。

Besides wanting to this chart in R I also want to use ggplot's faceting,i.e. put two stacked barcharts side by side in ggplot to compare two groups (A and B).I've played around with other charts and this seems the best choice. This is the data.The Excel chart only shows group A (the numbers are percentages).

D<-as.data.frame(structure(list(Group = c("A", "A", "A", "A", "A", 
"A", "B", "B", "B", "B", "B", "B"
), Education = c("NVQ Level 4 and above", "NVQ Level3", "NVQ Level 2", 
"Below NVQ Level 2", "Other qualification", "No qualification", 
"NVQ Level 4 and above", "NVQ Level3", "NVQ Level 2", "Below NVQ Level 2", 
"Other qualification", "No qualification"), Full.Time = c(47, 
27, 23, 17, 18, 9, 36, 26, 22, 22, 27, 12), PT.16.hours = c(20, 
24, 22, 18, 18, 12, 22, 21, 21, 22, 14, 10), PT.16.hours.1 = c(12, 
11, 10, 11, 13, 5, 24, 25, 25, 20, 16, 12)), .Names = c("Group", 
"Education", "Full.Time", "PT>16.hours", "PT<16.hours")))

在进行表面分析之前,团体,我实际上遇到麻烦创建一个单一的堆积条形图(如上面的那个)与克gplot2。我猜我不应该有3个变量(全时,PT,PT> 16小时),而是每个个案的单行,所以不要有

Before getting to the faceting to show the difference in the two groups, I'm actually having troubles creating a single stacked bar chart (like the one above) with ggplot2. I'm guessing I shouldn't have 3 variables (FullTime,PT,PT>16 hours), but rather single rows for each case, so instead of having

A    NVQ Level 4 and above      47  20  12
A    NVQ Level3                 27  24  11

我应该有

I should have

Group          Education    Work     Percentage
A   NVQ Level 4 and above   Full Time   47
A   NVQ Level 4 and above   PT>16 hours 20

如果这是唯一的方法为了让ggplot做图表,你将如何用一些代码行从一种格式转换到另一种格式?我经常发现这种类型的数据,所以有一个标准化的过程是很好的。我也玩过ggplot选项身份证,但没有取得太大成功。

If this is the only way to get ggplot to do the chart, how would you change from one format to the other with a few lines of code?I often find this type of data so it would be good to have a standardised procedure. I've also played around with the ggplot option 'identity' but haven't had much success.

任何帮助都将非常感谢。

Any help would be much appreciated.

谢谢

Thanks

推荐答案

重塑

reshape your data:

library(reshape2)
df <- melt(D)
And simply plot it :)

ggplot(df,aes(x = factor(Education),y = value,fill = factor(variable)))+
geom_bar()+ facet_grid(。〜Group)+
ylab('') + xlab('')+ opts(title ='')+ scale_fill_discrete('')+
theme_bw()+
opts(axis.text.x = theme_text(angle = 45,hjust = 1 ,vjust = 1))

ggplot(df, aes(x = factor(Education), y = value, fill = factor(variable))) + geom_bar() + facet_grid(.~Group) + ylab('') + xlab('') + opts(title = '') + scale_fill_discrete('') + theme_bw() + opts(axis.text.x=theme_text(angle = 45, hjust = 1, vjust = 1))

在第一行创建集合美学的地方,第二行添加 bar 图层和 facet ,在第三行,我们从图中删除不需要的文本,第四行设置 b& w 主题,在最后一行我们旋转x asis标签s。

Where the first line creates sets aesthetics, second line adds bar layer and the facet, on the 3rd line we remove unwanted texts from the plot, the 4th line sets the b&w theme and on the last line we rotate the x asis labels.

这篇关于R堆积条形图与汇总数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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