R面板时间序列平均值

时间:2016-08-17 10:00:16

标签: r ggplot2 time-series panel mean

我有ID = 1,2,3 ...年份= 2007,2008,2009的面板数据......以及因子foreign = 0,1和变量X.

我想创建一个时间序列图,其中x轴=年,y轴= X值,用于比较每个因子随时间的平均值(=平均值)。由于有两个因素,应该有两条线,一条是实线,一条是虚线。

我假设第一步涉及计算每年的均值和X因子,即在面板设置中。第二步应该是这样的:

ggplot(data, aes(x=year, y=MEAN(X), group=Foreign, linetype=Foreign))+geom_line()+theme_bw() 

非常感谢。

1 个答案:

答案 0 :(得分:0)

使用dplyr计算均值:

library(dplyr)

# generate some data (because you didn't provide any, or any way or generating it...)
data = data.frame(ID = 1:200, 
                  year = rep(1951:2000, each = 4), 
                  foreign = rep(c(0, 1), 100), 
                  x = rnorm(200))

# For each year, and seperately for foreign or not, calculate mean x.
data.means <- data %>% 
                group_by(year, foreign) %>%
                summarize(xmean = mean(x))

# plot. You don't need group = foreign
ggplot(data.means, aes(x = year, y = xmean, linetype = factor(foreign))) + 
  geom_line() +
  theme_bw()

ggplot2 lineplot