Reputation: 3
I want to plot impulse responses on the yield curve for different periods, i.e. x axis is the maturity, y axis the yields and the colors and the legend showing the change of the curve.
Until now the best I got is this way, which does the job, but is probably not really fancy, since I have to write every curve in a separate line. I would also prefer different colors. I don’t have much experience with ggplot, does someone have a better idea how to do it?
Thank you very much!
library(ggplot2)
yields <- matrix(c(0.00295373114921027 , -0.0459421137945784 , -0.0624168499151604 , -0.067980932937789 ,
-0.0694053937287238 , -0.0692517329677891 , -0.0685156201539083 , -0.067582403274368 ,
-0.0665988423594652 , -0.0656189053217228 , -0.0646608312532751 , -0.0637293717083627 ,
-0.0628244027548776 , -0.0619442197044327 , -0.0610867666747859 , -0.0602500702135424 ,
-0.0594323707193056 , -0.0586321427439522 , -0.0578480774357971 , -0.0570790546784645 ,
-0.0563241148596215 , -0.00215459356668265 , -0.0503525358273081 , -0.0661569400587159 ,
-0.0711300477300316 , -0.0720487787981406 , -0.071467793302907 , -0.0703726326391183 ,
-0.0691384953588887 , -0.0679030277920252 , -0.0667123544990393 , -0.0655780848054318 ,
-0.0644994089876531 , -0.0634715520359879 , -0.0624889225872946 , -0.061546216131371 ,
-0.0606387418827721 , -0.0597624650143631 , -0.0589139528071505 , -0.0580902955614903 ,
-0.0572890277058378 , -0.0565080572408784 , -0.0114030149586297 , -0.0584364000177077 ,
-0.0731467076374127 , -0.0771598014960408 , -0.0772563025592222 , -0.0759783133187364 ,
-0.0742949756521288 , -0.0725654971972243 , -0.0709132758481899 , -0.0693721842312216 ,
-0.0679434530580234 , -0.0666175347130694 , -0.0653823133748092 , -0.064226032252995 ,
-0.0631382053700173 , -0.0621097827523373 , -0.0611330574071095 , -0.0602014987728452 ,
-0.0593095798617343 , -0.0584526203664026 , -0.0576266511922522 , -0.0259039550645722 ,
-0.0713392729159159 , -0.0846135642132683 , -0.0873821445598901 , -0.0864145770618374 ,
-0.0842321656248465 , -0.0817822181406542 , -0.0794038971632835 , -0.0772030825995098 ,
-0.0751986434845017 , -0.0733790654241345 , -0.0717240054930198 , -0.0702122082535117 ,
-0.0688241737840042 , -0.0675428480624082 , -0.0663536015600818 , -0.0652439798164507 ,
-0.0642034060524481 , -0.0632228988915533 , -0.062294823906057 , -0.0614126814607235 ,
-0.0356906697272674 , -0.0802057443516945 , -0.0927109172869258 , -0.0948312709232168 ,
-0.0933160359662884 , -0.0906707200984785 , -0.0878297826989717 , -0.0851217074473752 ,
-0.0826433982580047 , -0.0804061321755991 , -0.078391948577664 , -0.0765750161787129 ,
-0.074929396405741 , -0.0734315854801917 , -0.0720611000264056 , -0.070800368098286 ,
-0.06963440635221 , -0.0685504609981612 , -0.0675376736237955 , -0.0665867889862895 ,
-0.0656899059030126 , -0.041924252769006 , -0.0859395512428242 , -0.0980690777105352 ,
-0.0998902231864448 , -0.0981312532275844 , -0.0952858061030871 , -0.0922804943873589 ,
-0.0894380032302587 , -0.0868506410877208 , -0.0845258989087087 , -0.082442633034258 ,
-0.0805723078349269 , -0.0788866738175846 , -0.0773602455734713 , -0.0759708363870938 ,
-0.0746994074652013 , -0.0735297103091648 , -0.0724478984462128 , -0.0714421685991245 ,
-0.0705024476083904 , -0.069620125612793 , -0.0457061968841903 , -0.0894736342214357 ,
-0.101451853061596 , -0.103170358605988 , -0.101338900579587 , -0.0984421938440188 ,
-0.0954018462443128 , -0.0925373018091543 , -0.0899385132976934 , -0.087611126218473 ,
-0.0855324891063465 , -0.083672807180883 , -0.0820027677128787 , -0.0804959823040618 ,
-0.0791294949139595 , -0.0778836100697815 , -0.076741518072849 , -0.0756888924723914 ,
-0.0747135192873169 , -0.0738049738647384 , -0.0729543455560101 , -0.0478882405946669 ,
-0.0915535484545184 , -0.103503081998629 , -0.105224282952248 , -0.103411538092442 ,
-0.100542800517279 , -0.0975363314039212 , -0.0947096872093228 , -0.0921516070869002 ,
-0.0898668712200736 , -0.0878321688856051 , -0.0860171844083944 , -0.0843921860976571 ,
-0.0829304460555512 , -0.0816087326146583 , -0.0804071268247377 , -0.0793086384190329 ,
-0.0782987958212864 , -0.077365269257024 , -0.0764975425795498 , -0.0756866337874161 ,
-0.0490683388923625 , -0.0927124062973295 , -0.10469631413649 , -0.106472504910454 ,
-0.104722941505346 , -0.101920387653774 , -0.0989807569184307 , -0.0962204462432715 ,
-0.0937275623772329 , -0.0915065084458646 , -0.0895337354309203 , -0.0877787743354477 ,
-0.0862117974893627 , -0.0848060222371294 , -0.0835381927940385 , -0.0823883891639117 ,
-0.08133963740236 , -0.0803774952091776 , -0.0794896715815857 , -0.078665695922331 ,
-0.0778966364240307 , -0.049642657409286 , -0.0933078697781386 , -0.105355312639458 ,
-0.107208920300852 , -0.105540680273448 , -0.102819310902584 , -0.0999590031086387 ,
-0.0972753956836941 , -0.0948562585750573 , -0.0927058561126891 , -0.0908006027790577 ,
-0.0891100514115876 , -0.0876044321695314 , -0.0862570434382154 , -0.0850447255214029 ,
-0.0839476639119731 , -0.0829489954626629 , -0.0820343909499394 , -0.0811916724780447 ,
-0.0804104809305694 , -0.0796819931570274 , -0.0498633299834566 , -0.093569851967338 ,
-0.105691353089729 , -0.107628651790965 , -0.106045701039253 , -0.103408011373276 ,
-0.10062845838066 , -0.0980221518341583 , -0.0956766755523155 , -0.0935962675088145 ,
-0.0917573966091565 , -0.0901297142955671 , -0.0886835747676271 , -0.0873924151254988 ,
-0.0862332222261255 , -0.0851863312935464 , -0.0842350287905035 , -0.0833651326198932 ,
-0.0825646078512673 , -0.0818232330119734 , -0.0811323165090252 , -0.0498858643575532 ,
-0.0936414542543238 , -0.105837769235224 , -0.107856796934385 , -0.106355961107537 ,
-0.103798184309161 , -0.101095303621711 , -0.0985620367564847 , -0.096285858744458 ,
-0.0942710311953491 , -0.0924941133777877 , -0.0909248829730386 , -0.0895338402489474 ,
-0.0882945788753391 , -0.0871842468202314 , -0.0861833409066547 , -0.0852753068437304 ,
-0.0844461174305103 , -0.0836838869032038 , -0.082978536313231 , -0.082321509391053), 21, 12)
tau <- c(3, 6, 12, 24, 36, 48, 60, 72, 84, 96, 108, 120)
# Plot
data <- data.frame(tau = tau, t(yields))
p <- ggplot(data, aes(x = tau))
p <- p + geom_line(aes(y = yields[1,], colour = 1))
p <- p + geom_line(aes(y = yields[2,], colour = 2))
p <- p + geom_line(aes(y = yields[3,], colour = 3))
p <- p + geom_line(aes(y = yields[4,], colour = 4))
p <- p + geom_line(aes(y = yields[5,], colour = 5))
p <- p + geom_line(aes(y = yields[6,], colour = 6))
p <- p + geom_line(aes(y = yields[7,], colour = 7))
p <- p + geom_line(aes(y = yields[8,], colour = 8))
p <- p + geom_line(aes(y = yields[9,], colour = 9))
p <- p + geom_line(aes(y = yields[10,], colour = 10))
p <- p + geom_line(aes(y = yields[11,], colour = 11))
p <- p + geom_line(aes(y = yields[12,], colour = 12))
p <- p + geom_line(aes(y = yields[13,], colour = 13))
p <- p + geom_line(aes(y = yields[14,], colour = 14))
p <- p + geom_line(aes(y = yields[15,], colour = 15))
p <- p + geom_line(aes(y = yields[16,], colour = 16))
p <- p + geom_line(aes(y = yields[17,], colour = 17))
p <- p + geom_line(aes(y = yields[18,], colour = 18))
p <- p + geom_line(aes(y = yields[19,], colour = 19))
p <- p + geom_line(aes(y = yields[20,], colour = 20))
p <- p + geom_line(aes(y = yields[21,], colour = 21))
p
Upvotes: 0
Views: 334
Reputation: 39603
Try this:
# Plot
library(reshape2)
data <- data.frame(tau = tau, t(yields))
data.m <- melt(data,id.vars = 'tau')
ggplot(data.m, aes(x = tau,y=value,color=variable))+geom_line()
Upvotes: 0
Reputation: 37953
ggplot likes long data a lot, while the yields matrix you posted is considered 'wide' data. Here is a way of converting it from a matrix:
df <- data.frame(
x = as.vector(col(yields)),
y = as.vector(yields),
group = as.vector(row(yields))
)
Next you can match the column numbers to the tau
variable as follows:
df$tau <- tau[df$x]
After which, plotting should be a lot easier:
ggplot(df, aes(tau, y, group = group)) +
geom_line(aes(colour = as.factor(group)))
Upvotes: 1
Reputation: 173988
You can make your code a lot simpler by reshaping your matrix into long format by transposing it with t
and using tidyr::pivot_longer
.
library(tidyr)
df <- pivot_longer(cbind(tau, as.data.frame(t(yields))), -1, names_to = "yield")
df$yield <- factor(substr(df$yield, 2, 4), levels = 1:21)
ggplot(df, aes(tau, value, colour = yield)) + geom_line()
Upvotes: 3