ggplot堆栈每个月的条形图前5名 [英] ggplot to stack bar graph top 5 for each month
本文介绍了ggplot堆栈每个月的条形图前5名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
df
Date Desc count
1/1/2012 A 10
1/1/2012 B 5
1/1/2012 C 7
1/1/2012 D 25
1/1/2012 E 19
1/1/2012 F 30
1/1/2012 G 50
2012/1/1 H 10
1/1/2012 I 100
2/1/2012 A 10
2/1/2012 B 5
2/1/2012 C 7
2/1/2012 D 25
2/1/2012 E 19
2/1/2012 F 30
2/1/2012 G 50
2 / 1/2012 H 10
2/1/2012 I 100
3/1/2012 A 1
3/1/2012 B 4
3/1/2012 C 5
3/1/2012 D 6
3/1/2012 E 6
3/1/2012 F 7
3/1/2012 G 8
3 / 1/2012 H 5
3/1/2012 I 10
我有类似的东西但这个曲线图中的所有值的:
ggplot(DF,AES(日期,计数))+ geom_bar(AES(填充= Desc),stat =identity,position =stack)+ me_bw()
解决方案
您有第一到子集数据:
库(plyr)
库(GGPLOT2)
df_top< - ddply(DF,(日期),
函数(x)head(x [order(x $ count,decrease = TRUE),],5))
ggplot(df_top,aes(Date,count))+
geom_bar(aes(fill = Desc),stat =identity,position =stack)+
theme_bw()
I have a good one. I've thinking about this a long time now. I have this data set and this data set could be be huge. I would like to graph a ggplot stack bar based on top 5 higest count for each month. For example, for 1//1/2012, the higest counts would be I, G, F, D and E.
df
Date Desc count
1/1/2012 A 10
1/1/2012 B 5
1/1/2012 C 7
1/1/2012 D 25
1/1/2012 E 19
1/1/2012 F 30
1/1/2012 G 50
1/1/2012 H 10
1/1/2012 I 100
2/1/2012 A 10
2/1/2012 B 5
2/1/2012 C 7
2/1/2012 D 25
2/1/2012 E 19
2/1/2012 F 30
2/1/2012 G 50
2/1/2012 H 10
2/1/2012 I 100
3/1/2012 A 1
3/1/2012 B 4
3/1/2012 C 5
3/1/2012 D 6
3/1/2012 E 6
3/1/2012 F 7
3/1/2012 G 8
3/1/2012 H 5
3/1/2012 I 10
I have something like this but this graphs all of the values:
ggplot(df, aes(Date, count))+ geom_bar(aes(fill=Desc), stat="identity", position="stack") + theme_bw()
解决方案
You have to subset the data first:
library(plyr)
library(ggplot2)
df_top <- ddply(df, .(Date),
function(x) head(x[order(x$count, decreasing = TRUE),], 5))
ggplot(df_top, aes(Date, count))+
geom_bar(aes(fill=Desc), stat="identity", position="stack") +
theme_bw()
这篇关于ggplot堆栈每个月的条形图前5名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文