Ryan Walter
Ryan Walter

Reputation: 271

Faceting a Dataset

This is a beginner question. I have spent most of the day trying to work out how to facet my data, but all of the examples of faceting that I have come across seem unsuited to my dataset.

Here are the first five rows from my data:

      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

I simply want to create two line charts. On both charts the x-axis will be the date. On the first chart, the y-axis should be Italy_v_Germany.Spread and on the second, the y-axis should be Greece_v_Germany.Spread.

The first chart looks like this:

enter image description here So I want the two charts to appear alongside each other, like this:

enter image description here The one on the left should be Italy_v_Germany.Spread, and the one on the right should be Greece_v_Germany.Spread.

I really have no idea where to start with this. Hoping that someone can point me in the right direction.

In the interest I making the example reproducible, I will share a link to the CSV files which I'm using: https://1drv.ms/u/s!AvGKDeEV3LOsmmlHkzO6YVQTRiOX?e=mukBVy. Unforunately these files convert into excel format when shared via this link, so you may have to export the files to CSVs so that the code works.

Here is the code that I have so far:

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") 

Upvotes: 0

Views: 53

Answers (1)

TimTeaFan
TimTeaFan

Reputation: 18581

You need to get your data into a long format, for example, by using pivot_wider. Then it should work.

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)

enter image description here

Upvotes: 4

Related Questions