首页 文章

R面板时间序列平均值

提问于
浏览
0

我有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

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

相关问题