简体   繁体   English

如何使用ggplot2按月,季度或年份分组

[英]How to group by month, quarter or year using ggplot2

Given the following case: 鉴于以下情况:

a <- c(rep("A", 25), rep("B", 25))
b <- rep(as.Date(c("2007-01-01")) + seq(60,1500,60),2)
c <- runif(50, 0, 1000)
d <- data.frame(a,b,c)

If I visualize this using ggplot2 I get a nice bar chart 如果我使用ggplot2将其可视化,我会得到一个漂亮的条形图

ggplot(data = d, aes(x=b, y=c, fill=a)) + 
geom_bar(stat = "identity") + 
scale_x_date(breaks = as.Date(c("2007-01-01", "2008-01-01", "2009-01-01", "2010-01-01", "2011-01-01")), 
minor_breaks = NULL)

1

Note: One bar represents one observation. 注意:一个条形代表一个观察。

However, I would like to be able to quickly group these observations together and have only 1 bar chart per year or 1 bar chart for every 2 years. 但是,我希望能够快速将这些观察结果组合在一起,每年只有1个条形图或每2年1个条形图。

How is it possible to do this using ggplot2? 如何使用ggplot2完成此操作?

ggplot2 does not do aggregation by itself. ggplot2本身不进行聚合。 You can use the base R aggregate function for it or packages like dplyr , plyr or data.table . 您可以使用基本R aggregate函数或dplyrplyrdata.table等包。

A data.table solution would be: data.table解决方案将是:

require(data.table)
require(ggplot2)
setDT(d)[,b := as.IDate(b)]
ggplot(d[,sum(c), by=.(a, year(b))], aes(x=year, y=V1, fill=a)) +
  geom_bar(stat = "identity")

Obviously you can take whatever statistic you like instead of sum and also group by something else then year in the d[,sum(c), by=.(a, year(b))] call. 显然你可以采取你喜欢的任何统计数据而不是sum ,然后在d[,sum(c), by=.(a, year(b))]调用中d[,sum(c), by=.(a, year(b))]年份分组。

The Result would be: 结果将是:

在此输入图像描述

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM