r - 面向数据集
问题描述
这是一个初学者的问题。我花了一天的大部分时间试图弄清楚如何对我的数据进行分面,但是我遇到的所有分面示例似乎都不适合我的数据集。
这是我数据的前五行:
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
。
我真的不知道从哪里开始。希望有人能指出我正确的方向。
为了使示例可重现,我将分享一个指向我正在使用的 CSV 文件的链接:https ://1drv.ms/u/s!AvGKDeEV3LOsmmlHkzO6YVQTRiOX?e=mukBVy 。不幸的是,这些文件在通过此链接共享时会转换为 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)
推荐阅读
- mysql - 防止脚本在 docker 容器中退出
- java-8 - 如何解决 Java 8 函数式编程中的不兼容类型错误
- java - 改造不包含参数
- javascript - 在特定的执行上下文(iframe)上运行 javascript
- python - 使用 tensorboard Python Tensorflow 显示模型的实时训练
- firefox - 你可以使用 Rust 编写 Firefox 附加组件/扩展吗?
- python - 重新采样总和保持每天熊猫最后一次观察的指数
- quanteda - Quanteda:给定 n-1 个前置词/类型,如何获取 ngram 及其频率
- php - Twig 语法 - 如何从嵌套的动态键中获取值
- android - 致命错误:sys/cdefs.h:编译 openssl-1.1.0h 时没有这样的文件或目录