0

我正在尝试在 x 轴由日期组成的函数中添加 geom_vline。该图显示了挪威克朗兑欧元,垂直线应该显示在政策利率变化的日期。以下代码不显示垂直线:

nok_eur_plot <- function(nok_eur_data, regression_method) {
  g <- ggplot(
      nok_eur_data, 
      aes(x = Date, y = NOK_EUR)
      ) + 
    geom_smooth(method = regression_method) +
    geom_point() + 
    labs(
      x = "Date", 
      y = paste("NOK per EUR for the last", length(nok_eur_data$NOK_EUR), "working-days", sep = " "),
      title = "NOK per EUR",
      subtitle = paste("From", min(nok_eur_data$Date), "to", max(nok_eur_data$Date), sep = " ")
      ) + 
    theme(
      axis.title.y = element_text(color = "blue")
      )

  rate_changes <- nok_key_policy_rate_change(length(nok_eur_data$NOK_EUR))

  for(row in 1:nrow(rate_changes)) {
    g + geom_vline(xintercept = rate_changes$Date[row], color = "red", size = 1, linetype = 4)
  }

  plot(g)
}

结果是这样的: 在此处输入图像描述

我试过改变

xintercept = rate_changes$Date[row]

xintercept = as.numeric(rate_changes$Date[row])

xintercept = as.POSIXct(rate_changes$Date[row])

如建议here,但无济于事。我检查了图中显示的日期范围内是否存在汇率变化,其中有 3 个。

整个 Rmd 脚本如下所示:

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library("xml2")
library("dplyr")
library("ggplot2")
library("scales")
```

## NOK vs EUR Regression

```{r, results='asis'}
analysis_periods <- c(10,100,200)

nok_eur <- function(days) {

  url_to_read <- paste(
    "https://data.norges-bank.no/api/data/EXR/",
    "B.EUR.NOK.SP?lastNObservations=",
    days,
    sep = ""
  )

  nok_eur_obs <- read_xml(url_to_read) %>%
    xml_find_all("//Obs")

  dates_closed <- nok_eur_obs %>% 
    xml_attr("TIME_PERIOD") %>% 
    as.Date("%Y-%m-%d")

  nok_eur_daily <- nok_eur_obs %>% 
    xml_attr("OBS_VALUE") %>% 
    as.numeric()

  nok_eur_data <- data.frame(x = dates_closed, y = nok_eur_daily)
  colnames(nok_eur_data) <- c("Date", "NOK_EUR")
  return(nok_eur_data)
}

nok_key_policy_rate_change <- function(days) {

  url_key_policy_rates <- "https://data.norges-bank.no/api/data/IR/B.KPRA.RR.R"

  key_policy_rates_obs <- read_xml(url_key_policy_rates) %>%
    xml_find_all("//Obs")

  key_policy_rates_subset <- key_policy_rates_obs[
    (length(key_policy_rates_obs) - days + 1):length(key_policy_rates_obs)
    ]

  dates_closed_rates <- key_policy_rates_subset %>%
    xml_attr("TIME_PERIOD") %>%
    as.Date("%Y-%m-%d")

  rates_daily <- key_policy_rates_subset %>%
    xml_attr("OBS_VALUE") %>%
    as.numeric()

  nok_key_policy_rate_data <- data.frame(x = dates_closed_rates, y = rates_daily)
  colnames(nok_key_policy_rate_data) <- c("Date", "Key_policy_rate")
  for(row in 1:nrow(nok_key_policy_rate_data)) {
    if(row == 1) {
      rate_change <- c(0)
    } else {
      change_from_previous <- nok_key_policy_rate_data$Key_policy_rate[row] - nok_key_policy_rate_data$Key_policy_rate[row - 1]
      rate_change <- c(rate_change, change_from_previous)
    }
  }
  nok_key_policy_rate_data["Change"] <- rate_change

  nok_key_policy_rate_change_data <- filter(nok_key_policy_rate_data, Change != 0)
  return(nok_key_policy_rate_change_data)

} 

nok_eur_plot <- function(nok_eur_data, regression_method) {
  g <- ggplot(
      nok_eur_data, 
      aes(x = Date, y = NOK_EUR)
      ) + 
    geom_smooth(method = regression_method) +
    geom_point() + 
    labs(
      x = "Date", 
      y = paste("NOK per EUR for the last", length(nok_eur_data$NOK_EUR), "working-days", sep = " "),
      title = "NOK per EUR",
      subtitle = paste("From", min(nok_eur_data$Date), "to", max(nok_eur_data$Date), sep = " ")
      ) + 
    theme(
      axis.title.y = element_text(color = "blue")
      )

  rate_changes <- nok_key_policy_rate_change(length(nok_eur_data$NOK_EUR))

  for(row in 1:nrow(rate_changes)) {
    g + geom_vline(xintercept = rate_changes$Date[row], colour = "red", size = 1, linetype = 4)
  }

  plot(g)
}

for(no_days in analysis_periods) {

  nok_eur_plot(nok_eur(no_days), "auto")
  nok_eur_plot(nok_eur(no_days), "lm")

}

nok_eur_data 数据框(以 5 天为例):

    Date <date> NOK_EUR <dbl>
1   2019-09-25  9.9310      
2   2019-09-26  9.9235      
3   2019-09-27  9.9155      
4   2019-09-30  9.8953      
5   2019-10-01  9.9463      
6   2019-10-02  9.9930  
...

nok_key_policy_rate_change_data 数据帧(以 100 天为例):

    Date <date> Key_policy_rate <dbl> Change <dbl>
1   2019-06-21  0.25                  0.25  
2   2019-09-20  0.50                  0.25  

任何建议都非常感谢。

4

1 回答 1

2

代码中的以下循环实际上不会修改图形:

for(row in 1:nrow(rate_changes)) {
    g + geom_vline(xintercept = rate_changes$Date[row], color = "red", size = 1, linetype = 4)
}

您应该分配结果(即g <- g + ...)以获得效果。

更高效:去掉for循环,一次性添加所有垂直线

g <- g + geom_vline(xintercept = rate_changes$Date, color = "red", size = 1, linetype = 4)
print(g)
于 2019-10-08T15:52:27.760 回答