R面板时间序列均值图

R panel time series mean plot

我有 ID=1,2,3... year=2007,2008,2009... 的面板数据和一个因子 foreign=0,1 和一个变量 X。

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

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

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

非常感谢。

使用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()