分面数据集

Faceting a Dataset

这是一个初学者问题。我一天中的大部分时间都在尝试弄清楚如何对我的数据进行分面,但我遇到的所有分面示例似乎都不适合我的数据集。

这是我数据的前五行:

      Date Germany.Yield Italy.Yield Greece.Yield Italy_v_Germany.Spread Greece_v_Germany.Spread
2020-04-19        -0.472       1.820        2.287                  2.292                   2.759
2020-04-12        -0.472       1.790        2.112                  2.262                   2.584
2020-04-05        -0.345       1.599        1.829                  1.944                   2.174
2020-03-29        -0.441       1.542        1.972                  1.983                   2.413
2020-03-22        -0.475       1.334        1.585                  1.809                   2.060

我只想创建两个折线图。在两个图表上,x 轴都是日期。在第一个图表上,y 轴应为 Italy_v_Germany.Spread,在第二个图表上,y 轴应为 Greece_v_Germany.Spread.

第一个图表如下所示:

所以我希望这两个图表并排显示,如下所示:

左边的应该是Italy_v_Germany.Spread,右边的应该是Greece_v_Germany.Spread.

我真的不知道从哪里开始。希望有人能指出我正确的方向。

为了使示例可重现,我将共享一个 link 到我正在使用的 CSV 文件:https://1drv.ms/u/s!AvGKDeEV3LOsmmlHkzO6YVQTRiOX?e=mukBVy。不幸的是,这些文件在通过此 link 共享时会转换为 excel 格式,因此您可能必须将文件导出为 CSV,以便代码正常工作。

这是我目前的代码:

library(ggplot2)
library(scales)
library(extrafont)
library(dplyr)
library(tidyr)

work_dir <- "D:\OneDrive\Documents\Economic Data\Historical Yields\Eurozone"
setwd(work_dir)

# Germany
#---------------------------------------
germany_yields <- read.csv(file = "Germany 10-Year Yield Weekly (2007-2020).csv", stringsAsFactors = F)
germany_yields <- germany_yields[, -(3:6)]
colnames(germany_yields)[1] <- "Date"
colnames(germany_yields)[2] <- "Germany.Yield"
#---------------------------------------

# Italy
#---------------------------------------
italy_yields <- read.csv(file = "Italy 10-Year Yield Weekly (2007-2020).csv", stringsAsFactors = F)

italy_yields <- italy_yields[, -(3:6)]
colnames(italy_yields)[1] <- "Date"
colnames(italy_yields)[2] <- "Italy.Yield"
#---------------------------------------

# Greece
#---------------------------------------
greece_yields <- read.csv(file = "Greece 10-Year Yield Weekly (2007-2020).csv", stringsAsFactors = F)

greece_yields <- greece_yields[, -(3:6)]
colnames(greece_yields)[1] <- "Date"
colnames(greece_yields)[2] <- "Greece.Yield"
#---------------------------------------

# Join data
#---------------------------------------
combined <- merge(merge(germany_yields, italy_yields, by = "Date", sort = F), 
              greece_yields, by = "Date", sort = F)

combined <- na.omit(combined)
combined$Date <- as.Date(combined$Date,format = "%B %d, %Y")
combined["Italy_v_Germany.Spread"] <- combined$Italy.Yield - combined$Germany.Yield
combined["Greece_v_Germany.Spread"] <- combined$Greece.Yield - combined$Germany.Yield
#--------------------------------------------------------------------

fl_dates <- c(tail(combined$Date, n=1), head(combined$Date, n=1))

ggplot(data=combined, aes(x = Date, y = Italy_v_Germany.Spread)) + geom_line() +

       scale_x_date(limits = fl_dates,
                    breaks = seq(as.Date("2008-01-01"), as.Date("2020-01-01"), by="2 years"),
                    expand = c(0, 0),
                    date_labels = "%Y") 

您需要将数据转换为长格式,例如,使用 pivot_wider。然后它应该工作。

library(dplyr)
library(tidyr)
library(ggplot2)


data <- tribble(~Date,  ~Germany.Yield, ~Italy.Yield, ~Greece.Yield, ~Italy_v_Germany.Spread, ~Greece_v_Germany.Spread,
"2020-04-19",        -0.472,       1.820,        2.287,                  2.292,                   2.759,
"2020-04-19",       -0.472,       1.820,        2.287,                  2.292,                   2.759,
"2020-04-12",      -0.472,       1.790,        2.112,                  2.262,                   2.584,
"2020-04-05",     -0.345,       1.599,        1.829,                  1.944,                   2.174,
"2020-03-29",    -0.441,       1.542,        1.972,                  1.983,                   2.413,
"2020-03-22",   -0.475,       1.334,        1.585,                  1.809,                   2.060
)


data %>% 
  mutate(Date = as.Date(Date)) %>% 
  pivot_longer(
    cols = ends_with("Spread"),
    names_to = "country",
    values_to = "Spread_v_Germany",
    values_drop_na = TRUE
  ) %>% 

  ggplot(., aes(x = Date, y = Spread_v_Germany, group = 1)) +
    geom_line() +
   facet_wrap(. ~ country)