上课日期的x轴上的ggplot geom_vline

时间:2019-10-08 14:31:47

标签: r ggplot2 geom-vline

我正在尝试在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)
}

结果是这样的: enter image description here

我尝试过更改

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  

任何建议都值得赞赏。

1 个答案:

答案 0 :(得分: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)