我需要一些帮助来制作不同比例的分面图,对于任何一个熟悉 facet_grid_sc
. 我已经按照这里提供的例子。https:/github.comzeehiofacetscales。但不幸的是,我还没有能够在我自己的数据上复制结果。
下面是我正在使用的数据集的快速查看。
Date Germany.Yield Italy.Yield Greece.Yield measure value
1 2020-04-19 -0.472 1.820 2.287 Italy_v_Germany.Spread 2.292
2 2020-04-12 -0.472 1.790 2.112 Italy_v_Germany.Spread 2.262
3 2020-04-05 -0.345 1.599 1.829 Italy_v_Germany.Spread 1.944
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
1381 2007-02-11 4.048 4.267 4.302 Greece_v_Germany.Spread 0.254
1382 2007-02-04 4.049 4.243 4.343 Greece_v_Germany.Spread 0.294
1383 2007-01-28 4.094 4.303 4.325 Greece_v_Germany.Spread 0.231
下面的代码产生了错误 "Invalid input: date_trans works with objects of class Date only". 我完全不知道是什么原因导致了这个错误,因为在我看来,我似乎已经实现了 "无效输入:date_trans仅适用于Date类的对象"。facet_grid_sc
正确。
library(ggplot2)
library(scales)
library(extrafont)
library(dplyr)
library(tidyr)
library(facetscales)
work_dir <- "D:\\OneDrive\\Documents\\Economic Data\\Historical Yields\\Eurozone"
setwd(work_dir)
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_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_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"
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))
longcombined <- gather(combined,
key="measure",
value="value",
c("Italy_v_Germany.Spread",
"Greece_v_Germany.Spread"))
scales_y <- list(Italy_v_Germany.Spread = scale_y_continuous(),
Greece_v_Germany.Spread = scale_y_continuous())
ggplot(data=longcombined, aes(x = Date, y = value)) + geom_line() +
facet_grid_sc(rows = vars(measure), scales = list(y = scales_y)) +
geom_blank(aes(y = 0)) +
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")
如果你想重现这个例子,这里是我使用的CSV文件。https:/onedrive.live.com?authkey=%21AEeTM7phVBNGI5c&id=ACB3DC15E10D8AF1%213433&cid=ACB3DC15E10D8AF。.
谢谢你的阅读。