-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode.R
62 lines (48 loc) · 2.43 KB
/
Code.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Load the required libraries
require("jpeg")
require("grid")
require("ggplot2")
# Load and format the raw data
URL <- "https://sdw-wsrest.ecb.europa.eu/service/data/ICP/M.ES.N.000000.4.INX?format=csvdata"
df <- read.csv(URL)[c("TIME_PERIOD", "OBS_VALUE")]
df$TIME_PERIOD <- seq.Date(as.Date("1996-02-01"), by="month", length.out = nrow(df)) - 1
df$OBS_VALUE <- as.numeric(df$OBS_VALUE)
df <- df[df$TIME_PERIOD >= as.Date("1999-12-31"),]
df$PPP <- df$OBS_VALUE[1]/df$OBS_VALUE
# Load the banknote image
bill <- readJPEG("Euro Banknote.jpg")
bill <- rasterGrob(bill, width=unit(1,"npc"), height=unit(1,"npc"), interpolate=TRUE)
# Draw the chart
graph <- ggplot(df, aes(x = TIME_PERIOD, y = PPP)) +
# First draw the banknote. You must set the starting position of axis Y "ymin"
annotation_custom(bill, xmin = -Inf, xmax = Inf, ymin = 0, ymax = Inf) +
# Then draw over it the white ribbon from the top of the graph to the data series
geom_ribbon(aes(ymin = PPP, ymax = Inf), fill = "white") +
geom_line(size=1, colour="#000000") +
geom_hline(yintercept=tail(df$PPP, 1), linetype="dashed") +
# Add the appropriate scale
scale_x_date(date_breaks="1 year",date_labels = "%Y", expand=c(0,0)) +
scale_y_continuous(expand = c(0,0), labels = scales::percent, limits = c(0,1), breaks = seq(0, 1, 0.1)) +
# Put the labels you want to the chart
labs(
title = "Poder Adquisitivo del €uro en España",
subtitle = "Desde 2000 hasta 2022. 1 de enero de 2000 = 100%",
x="",
y="",
caption="Elaboración propia.Fuente: ECB Statistical Data Warehouse. Creado por @jaldeko.") +
# Add a theme and other aesthetics modifications
theme_minimal() +
theme(plot.caption = element_text(face="italic"),
plot.title = element_text(face="bold", hjust=0.5, size=rel(1.5)),
plot.subtitle = element_text(face="italic", hjust=0.5),
axis.text.x = element_text(colour = "black", hjust=-0.5, face="bold"),
axis.text.y = element_text(colour = "black", face="bold"),
plot.margin = unit(c(0.5,0.5,0.5,0.5), "cm"),
panel.grid.major.x = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour="black"),
axis.ticks = element_line(colour="black"),
legend.key.height = unit(0.75, 'cm'),
legend.text = element_text(colour = "black", face="italic", size=rel(0.75)))
# Plot it!
graph