代码之家  ›  专栏  ›  技术社区  ›  Hercules Apergis

如何在R中绘制贝弗里奇曲线

  •  -1
  • Hercules Apergis  · 技术社区  · 8 年前

    假设两个时间序列:

    myvector<-rnorm(10,1,72)
    myvector1<-rnorm(5,1,72)
    myts <- ts(myvector, start=c(2009, 1), end=c(2014, 12), frequency=12) 
    myts1 <- ts(myvector1, start=c(2009, 1), end=c(2014, 12), frequency=12) 
    

    我如何创建一个像图中那样的图,其中每个变量都位于图的轴上,每个点代表一天(季度或一年)。该曲线图也称为贝弗里奇曲线图。欢迎提出任何建议。

    The desired plot

    1 回复  |  直到 8 年前
        1
  •  0
  •   Nar    8 年前

    你可以使用ggplot,比如:

       library(ggplot2)     
       df <- as.data.frame(cbind(myts, myts1))
    
       ggplot(data=df)+
       geom_line(data=df, aes(x=myts, y=myts1, group=1), linetype = 1)
    
        2
  •  0
  •   Michael Willox    4 年前

    贝弗里奇曲线图是散点图,其中一条线连接顺序点,代表某一时期的职位空缺率和失业率。使用不同的线颜色有助于区分曲线沿线的运动,反映劳动力市场紧张程度的变化,以及曲线中靠近或远离原点的变化,代表劳动力市场效率的变化。您可以将geom_点和geom_路径与ggplot结合起来进行区分。如果使用geom_线而不是geom_路径,线可能会通过空间上比时间更近的点,从而歪曲信息。我将可复制数据限制为两个周期,但你应该看到它们与BLS的原始图像非常匹配。

    library(tidyverse)
    library(zoo)
    library(lubridate)
        
      Month <- format(c('2007-12-01','2008-01-01','2008-02-01','2008-03-01','2008-04-01','2008-05-01','2008-06-01','2008-07-01','2008-08-01','2008-09-01','2008-10-01','2008-11-01','2008-12-01','2009-01-01','2009-02-01','2009-03-01','2009-04-01','2009-05-01','2009-06-01','2020-05-01','2020-06-01','2020-07-01','2020-08-01','2020-09-01','2020-10-01','2020-11-01','2020-12-01','2021-01-01','2021-02-01','2021-03-01','2021-04-01','2021-05-01','2021-06-01','2021-07-01','2021-08-01','2021-09-01','2021-10-01','2021-11-01'), format="%Y-%b-%d")
      
      Job.openings.rate <- c(3.2,3.2,3,3,2.8,3,2.7,2.7,2.6,2.3,2.4,2.3,2.3,2,2.1,1.9,1.7,1.9,1.9,3.9,4.2,4.6,4.4,4.5,4.6,4.5,4.5,4.7,5,5.4,6,6.1,6.5,7,6.7,6.7,7,6.8)
      
      Unemployment.rate <- c(5,5,4.9,5.1,5,5.4,5.6,5.8,6.1,6.1,6.5,6.8,7.3,7.8,8.3,8.7,9,9.4,9.5, 13.3,11.1,10.2,8.4,7.8,6.9,6.7,6.7,6.3,6.2,6,6.1,5.8,5.9,5.4,5.2,4.8,4.6,4.2)
      
    dat <- data.frame(Month,Job.openings.rate,Unemployment.rate)
      dat$Month <- as.Date(dat$Month, origin = "1899-12-30")
      dat$Month <- strptime(dat$Month, "%Y-%m-%d")
      
      dat <- dat %>% 
        dplyr::mutate(period = case_when(
                                         Month>=ymd('2007-12-01') & Month<ymd('2009-07-01') ~  1,
                                         Month>=ymd('2020-05-01') & Month<=ymd('2021-11-01') ~  2))
      
      dat$period <- factor(dat$period, levels=c(1,2))
      
      cbp2 <- c( "orange", "black")
      names <- c("1" = "Dec 2007 to Jun 2009", 
                 "2" = "May 2020 to Nov 2021")
      
    bc <-  dat %>% ggplot() +
        geom_point(aes(x=Unemployment.rate, y=Job.openings.rate, group=period, colour=period)) + 
        geom_path(aes(x=Unemployment.rate, y=Job.openings.rate, group=period, colour=period)) +
        ggtitle("The Beveridge Curve (job openings rate vs. unemployment rate), seasonally adjusted") +
        scale_color_manual(values = cbp2, labels=names) +
        scale_fill_manual(values = cbp2, labels=names) +
        xlab("Unemployment rate") +
        ylab("Job openings rate") +
        theme_minimal() +
        theme(legend.title=element_blank()) +
        theme(legend.position = c(.8, .75)) +
        theme(panel.background = element_blank()) +
        theme(panel.border = element_rect(colour = "black", fill=NA, size=.5))
        theme(legend.position = "top", legend.title.align = "1", legend.title = element_text("Legend"))
    

    enter image description here

    下面的数据和原始图像来自BLS,BLS每月更新图像和数据。 https://www.bls.gov/charts/job-openings-and-labor-turnover/job-openings-unemployment-beveridge-curve.htm

    enter image description here