Customising the IRF plot - r

I am trying to customise the plot generated by:
plot(irf(VECMcoeff, n.ahead = 20, cumulative = FALSE, ortho = TRUE))
Current figure:
Not yet allowed to post figures.
Is there a way, using R's basis plotting functions, to adjust this plot? More specifically;
I would like to have plots stand-alone from each other.
Adjust x- and y-axis titles.
Adjust the main title.
Remove the '95 % Bootstrap CI, 100 runs'.
Thanks for any help!
Data:
dput(head(combined,25))
structure(c(3.12378036948822, 3.24514490963516, 2.54231015523096,
3.10758964326189, 3.26905177146087, 3.39086921629928, 3.39867627597089,
3.063339608249, 2.82158440194456, 3.00060851536641, 2.87498214357332,
2.73447964251719, 2.51961774067125, 2.43535838893541, 2.53536474393679,
2.11458263713232, 2.08443293370433, 1.70951997715485, 1.6939353104687,
1.99402766681289, 2.17851574489578, 2.02035721460859, 2.19849725222166,
2.12385225312224, 2.13870052300126, 2.53563259854902, 2.71236841778707,
2.80602806173539, 2.44978220282482, 2.22240349195674, 2.6269002941349,
2.55424892433652, 2.84227347851153, 3.00695212249206, 2.56409065301929,
2.11958065079056, 1.93021828518557, 1.91149187923047, 2.12824458610721,
1.99034383037538, 1.85993728242216, 1.78831122085649, 1.70508421574581,
1.34148894168009, 1.26428948883955, 1.53707667916106, 1.40125321322403,
1.56189928398736, 1.59267901471992, 1.29435444758231, 2.88357952825106,
3.2967949657277, 2.71315870827614, 2.88194083947586, 2.55384396254808,
2.48162552588286, 2.43461752858767, 2.60895931242784, 2.88699097436266,
3.06774759389068, 2.92820858177705, 1.9236817467793, 1.30469143981917,
1.63412478606386, 1.32569634585868, 1.66411340281953, 1.811114177636,
1.32324449480086, 0.683740288067047, 0.506428412402278, 0.244160570695116,
-0.0614637978267916, 0.11100051693192, 0.107431188637327, -0.0946163941762501,
1.56887584570782, 2.2953989716194, 2.3913948824343, 1.60366568545365,
2.14074303245166, 1.42821783272864, 1.14416900596202, 1.32550310805691,
1.06775704738626, 1.1754985484452, 1.30819594680082, 1.57801107586324,
1.57465869540119, 1.52953051921855, 1.59632502092932, 1.51164066108273,
1.74699133577352, 1.89513403376172, 1.50403737650093, 1.69077755145674,
1.51619819345532, 1.7908456553931, 1.63120428277988, 1.72264300428443,
1.91016040082409, 2.93953881174612, 0.573867521584496, 1.36693966408554,
1.33745582274447, 2.00217541671565, 1.47500074486359, 1.54892810099376,
1.52596101747453, 1.85097710190023, 1.8027452973638, 1.71255671138078,
1.78801314649281, 1.73039561596535, 1.7797925346833, 1.68662137367852,
2.10887254895115, 2.47630376444312, 2.10728662380876, 1.99939507617536,
2.1661652656972, 1.97780409080129, 2.08116163569287, 2.33934227442197,
2.38773088163046, 2.39899888596041), .Dim = c(25L, 5L), .Dimnames = list(
NULL, c("rstar.nl2", "rstar.ger2", "rstar.fr2", "rstar.sp2",
"rstar.it2")))

Somewhat general advice:
plot() is a generic function that actually calls a more specific function (called a "method") depending on what you are trying to plot (see this chapter from Hadley Wickham's Advanced R book for details). In this case, you are feeding-in an object of class "varirf" to plot(). You can see this by running, e.g.:
out <- irf(your_arguments_go_here)
class(out)
The generic function plot() is calling the method plot.varirf() because you are feeding in an object of type "varirf". To see which parameters of plot.varirf() you can control, check out that function's help page
?plot.varirf
If this doesn't provide you with sufficient control over what you want your plot to look like, then you should abandon trying to use plot.varif() and construct your desired plot manually, as in:
plot(x=my_x_vals, y=my_y_vals, main="My Title", pch=20, col="red", etc.)
In manually creating your plot, you may find it useful to see how plot.varirf plots are created so you can implement some of the same formatting. To view the source code for plot.varirf, use:
getAnywhere(plot.varirf)

Here is an example:
plot(x, plot.type = "single", names = NULL, main = "IRF to a one-standard deviation shock to APP", sub = NULL, lty = NULL, lwd = NULL, col = NULL, ylim = NULL, ylab = "Eonia", xlab = "Number of periods", mar.multi = c(0, 4, 0, 4), oma.multi = c(6, 4, 6, 4), adj.mtext = NA, padj.mtext = NA, col.mtext = NA)
where, x is your varest object,
Good luck!

Related

labels for boxplot and barplot loop in r [duplicate]

I am trying to plot 16 boxplots, using a for loop. My problem is, that the 2nd title is plotted on the first plot, the 3rd title on the second plot and so forth.
Does anyone have a guess on, what I am doing wrong?
My code is the following:
boxplot(data$distance[data$countryname=="Sweden"]~data$alliance[data$countryname=="Sweden"],title(main = "Sweden"))
boxplot(data$distance[data$countryname=="Norway"]~data$alliance[data$countryname=="Norway"],title(main = "Norway"))
boxplot(data$distance[data$countryname=="Denmark"]~data$alliance[data$countryname=="Denmark"],title(main = "Denmark"))
boxplot(data$distance[data$countryname=="Finland"]~data$alliance[data$countryname=="Finland"],title(main = "Finland"))
boxplot(data$distance[data$countryname=="Iceland"]~data$alliance[data$countryname=="Iceland"],title(main = "Iceland"))
boxplot(data$distance[data$countryname=="Belgium"]~data$alliance[data$countryname=="Belgium"],title(main = "Belgium"))
boxplot(data$distance[data$countryname=="Netherlands"]~data$alliance[data$countryname=="Netherlands"],title(main = "Netherlands"))
boxplot(data$distance[data$countryname=="Luxembourg"]~data$alliance[data$countryname=="Luxembourg"],title(main = "Luxembourg"))
boxplot(data$distance[data$countryname=="France"]~data$alliance[data$countryname=="France"],title(main = "France"))
boxplot(data$distance[data$countryname=="Italy"]~data$alliance[data$countryname=="Italy"],title(main = "Italy"))
boxplot(data$distance[data$countryname=="Spain"]~data$alliance[data$countryname=="Spain"],title(main = "Spain"))
boxplot(data$distance[data$countryname=="Portugal"]~data$alliance[data$countryname=="Portugal"],title(main = "Portugal"))
boxplot(data$distance[data$countryname=="Germany"]~data$alliance[data$countryname=="Germany"],title(main = "Germany"))
boxplot(data$distance[data$countryname=="Austria"]~data$alliance[data$countryname=="Austria"],title(main = "Austria"))
boxplot(data$distance[data$countryname=="Ireland"]~data$alliance[data$countryname=="Ireland"],title(main = "Ireland"))
boxplot(data$distance[data$countryname=="UK"]~data$alliance[data$countryname=="UK"],title(main = "UK"))
I think this could replace all your lines and fix your problem:
for (i in data$countryname)
boxplot(distance~alliance, subset(data, countryname==i), main=i)
But that's hard to verify without a reproducible example or some of your data.frame.
Based on the documentation, you should be assigning a title to your boxplots by making explicit calls to the function title(), rather than as a parameter in the call to boxplot(). The first two calls to generate your boxplots should look something like the following:
boxplot(data$distance[data$countryname=="Sweden"]~data$alliance[data$countryname=="Sweden"])
title(main = "Sweden")
boxplot(data$distance[data$countryname=="Norway"]~data$alliance[data$countryname=="Norway"])
title(main = "Norway")

Why am I getting two ellipses when using the dataEllipse function in R?

I am creating a principle coordinates plot from DAPC data using the adegenet package in R. The data used for the plot are:
> dapc_MG$ind.coord
LD1 LD2
MGUD1 -9.405524 -0.092446808
MGUD2 -10.178221 -0.077018828
MGUD3 -8.004493 -0.223172024
MGUD4 -8.740758 -0.091989157
MGUD5 -8.891316 -0.136248422
MGUD6 -8.105366 0.957238296
MGUD7 -8.832198 -0.345634729
MGUD8 -8.484260 0.924628880
MGUD9 -8.978832 -0.126596011
MGUD10 -8.729387 -0.069143482
MGUD11 -7.211637 -0.104404732
MGUD12 -6.345985 -0.057321959
MGUD13 -8.660017 -0.113205115
MGUD14 -7.892150 -0.017548789
MGUD15 -8.813235 -0.073362348
MGUD16 -10.137858 -0.034488530
MGUD17 -8.462890 -0.069646135
MGUD18 -7.414354 -0.435114178
MGUD19 -10.067263 -0.109486111
MGUD20 -4.815789 -0.069918242
MGMD1 4.649435 0.149936267
MGMD2 4.074587 -1.355723783
MGMD3 3.862391 -1.150245800
MGMD4 5.095276 -0.243453670
MGMD5 3.388603 1.279395536
MGMD6 4.587423 0.110122511
MGMD7 5.805981 -1.688136390
MGMD8 5.992032 -1.625957602
MGMD9 3.836499 -0.429911284
MGMD10 3.632776 -0.663692457
MGMD11 4.846231 -0.620827242
MGMD12 4.648728 0.380656735
MGMD13 4.675340 0.096098822
MGMD14 4.109009 2.054348252
MGMD15 5.423083 1.925953017
MGMD16 5.661753 -1.321634663
MGMD17 4.473997 0.227259783
MGMD18 5.234144 2.154111222
MGMD19 5.347463 -1.500364562
MGMD20 4.844948 -2.150318949
MGLD1 3.750089 0.136716341
MGLD2 2.314980 -0.002332376
MGLD3 2.404047 0.001578862
MGLD4 2.878247 -0.574501378
MGLD5 4.289025 0.221731980
MGLD6 4.072082 -1.314282541
MGLD7 4.448515 -1.535511761
MGLD8 4.770242 1.864984067
MGLD9 4.727431 0.199772954
MGLD10 3.665372 -0.514571290
MGLD11 3.761236 0.118478439
MGLD12 2.740715 -0.779793448
MGLD13 4.993115 1.815394978
MGLD14 3.418433 -0.065471486
MGLD15 4.092151 1.864439810
MGLD16 4.390683 -0.201531790
MGLD17 3.004646 -0.807754677
MGLD18 3.453202 0.438770000
MGLD19 2.001394 1.956932251
MGLD20 4.806231 1.914213749
Using R's base plot package, I have generated the basic plot I want: Plot_no_ellipse
plot(dapc_MG$ind.coord[,"LD1"], dapc_MG$ind.coord[,"LD2"],
col=cols_MG[grpMG$grp], pch=c(0:2)[pop(di_MG)],
lwd = 2, xlab="axis1", ylab="axis2")
# Change xlim and ylim to fit
plot(dapc_MG$ind.coord[,"LD1"], dapc_MG$ind.coord[,"LD2"],
col=cols_MG[grpMG$grp], pch=c(0:2)[pop(di_MG)],
lwd = 2, xlab="axis1", ylab="axis2", xlim=c(-11,10), ylim=c(-5, 5))
#Add background to plot
rect(par("usr")[1],par("usr")[3],par("usr")[2],par("usr")[4],col = "lightgray")
points(dapc_MG$ind.coord[,"LD1"], dapc_MG$ind.coord[,"LD2"],
col=cols_MG[grpMG$grp], pch=c(0:2)[pop(di_MG)],
lwd = 2, xlab="axis1", ylab="axis2", xlim=c(-11,10), ylim=c(-5, 5))
legend("topright", c(popsMG, clustersMG),
col = c(rep('black', 3), cols_MG), pch = c(0:2, rep(15, 5)),
lty = FALSE, lwd = 2, cex=0.764, pt.cex = 1.5, y.intersp=1.5, bg = "lightgray")
Using dataEllipse from the car package, I'm trying to add group ellipses similar to this:
The function works as desired, but the output from the command I'm running produces a smaller ellipse inside the larger ones I am trying to draw: Plot_with_ellipse
dataEllipse(x = dapc_MG$ind.coord[,"LD1"], y = dapc_MG$ind.coord[,"LD2"], groups = grpMG$grp,
center.pch = FALSE, grid = FALSE, plot.points = FALSE, add = TRUE,
col = cols_MG, lwd = 1)
I've thoroughly checked the documentation for dataEllipse and I can't seem to identify why this duplication is occurring. I'm sure its not the default, as it doesn't happen with the example data they use. I assume that it must be something to do with how my data are structured, but I'm a but green when it comes to data wrangling. Could anyone help me identify the source of my issue?

Performance Analytics drawdown function not working r

I am trying to plot a drawdown graph of daily profits using the Performance Analytics package. I have managed to do this with the code:
cols = rainbow(ncol(pdrawdown),s=0.7, v=0.8, alpha= 0.7)
chart.Drawdown(pdrawdown, legend.loc = "bottomleft",colorset = cols,
main = "Drawdown Chart", xlab ="Date", ylab = "Drawdown")
for percentage returns. However when I use profits (which deals with much larger numbers), the graphics window doesn't even show any table, and no error or warning messages are produced.
My pdrawdown data is structured like so:
structure(list(Arbitrage = c(-410344.36040002, 43186613.0914002,
-56243745.8289002, 33212085.9369, 2685633.86650004, 52056396.8137002
), Cmdty = c(-5661740.59000004, -12816611.3327999, -15271367.5797001,
30328698.7996001, 2346206.95040001, -20111667.3121), Cnvt = c(-0.448500000005879,
-4.09389999999985, -3.76900000000023, -4.46530000000348, 9.47310000000289,
5.57809999999154), FI = c(-4959851.92789985, 51301719.2496983,
19347533.8012021, -45928596.3382014, -126566982.481699, 7039919.16710053
), IndexArb = c(465514.007300064, 712460.314099789, -1241298.64239982,
859103.288600107, -9142.46960010222, 1670160.15220016), OptVolD = c(3207463.8402,
-2645827.2004, 1917467.1194, -1645717.199, 1346643.9976, 1249267.9222
), OptVolG = c(1809178.81009999, -1247076.75579998, 1208738.92329999,
-998966.340099988, 474978.958799993, 2312496.41480001), Other = c(3121114.45319998,
-7558428.4574, 28751941.5262, -8287057.41060003, -5057308.97439997,
25541692.1845), RVG = c(107064420.2606, 41122417.5658004, -33634242.2959002,
-25318480.7920002, -9396822.53510034, 69874891.7186005), SAAsia = c(-31022426.4966,
-1381533.43030003, 21484053.8479001, 15456037.1175, 11307323.7993999,
8081090.74740009)), .Names = c("Arbitrage", "Cmdty", "Cnvt",
"FI", "IndexArb", "OptVolD", "OptVolG", "Other", "RVG", "SAAsia"
), row.names = c("2015-08-03", "2015-08-04", "2015-08-05", "2015-08-06",
"2015-08-07", "2015-08-10"), class = "data.frame")
Update: Setting a ylim value i.e.
cols = rainbow(ncol(pdrawdown),s=0.7, v=0.8, alpha= 0.7)
chart.Drawdown(pdrawdown, legend.loc = "bottomleft",colorset = cols,
main = "Drawdown Chart", xlab ="Date", ylab = "Drawdown",ylim = c(-1,0))
causes the graph to appear but it still displays effectively nonsensical data, no matter what my lower bound is for the ylim. With returns, however, this ylim term works as expected, fixing the axes of the plot.
Since no-one was able to give an answer, I will post my best solution in case anyone else runs into this problem in the future.
As far as I can tell, the issue is (as expected) simply being caused because the drawdown PnL is too large. My "hack" to fix this was to divide the datatable by a number that causes all values to be below 1. In my case this meant my y scale being in billions like so:
pdrawdown = pdrawdown/1000000000 # PnL displays in billions (set units here)
cols = rainbow(ncol(pdrawdown),s=0.7, v=0.8, alpha= 0.7)
chart.Drawdown(pdrawdown, legend.loc = "bottomleft",colorset = cols,
main = "Drawdown Chart", xlab ="Date", ylab = "Drawdown ($billions)
Dividing by any number smaller just gave a very odd looking graph with massive jumps that did not resemble the returns graph for the same data.
I suspect this is because the drawdown graph is designed for percentages and thus can only deal with numbers between 0 and -1 (maximum drawdown in percentage being, of course, -100%).

Title is missing in 1st boxplot when using a for loop

I am trying to plot 16 boxplots, using a for loop. My problem is, that the 2nd title is plotted on the first plot, the 3rd title on the second plot and so forth.
Does anyone have a guess on, what I am doing wrong?
My code is the following:
boxplot(data$distance[data$countryname=="Sweden"]~data$alliance[data$countryname=="Sweden"],title(main = "Sweden"))
boxplot(data$distance[data$countryname=="Norway"]~data$alliance[data$countryname=="Norway"],title(main = "Norway"))
boxplot(data$distance[data$countryname=="Denmark"]~data$alliance[data$countryname=="Denmark"],title(main = "Denmark"))
boxplot(data$distance[data$countryname=="Finland"]~data$alliance[data$countryname=="Finland"],title(main = "Finland"))
boxplot(data$distance[data$countryname=="Iceland"]~data$alliance[data$countryname=="Iceland"],title(main = "Iceland"))
boxplot(data$distance[data$countryname=="Belgium"]~data$alliance[data$countryname=="Belgium"],title(main = "Belgium"))
boxplot(data$distance[data$countryname=="Netherlands"]~data$alliance[data$countryname=="Netherlands"],title(main = "Netherlands"))
boxplot(data$distance[data$countryname=="Luxembourg"]~data$alliance[data$countryname=="Luxembourg"],title(main = "Luxembourg"))
boxplot(data$distance[data$countryname=="France"]~data$alliance[data$countryname=="France"],title(main = "France"))
boxplot(data$distance[data$countryname=="Italy"]~data$alliance[data$countryname=="Italy"],title(main = "Italy"))
boxplot(data$distance[data$countryname=="Spain"]~data$alliance[data$countryname=="Spain"],title(main = "Spain"))
boxplot(data$distance[data$countryname=="Portugal"]~data$alliance[data$countryname=="Portugal"],title(main = "Portugal"))
boxplot(data$distance[data$countryname=="Germany"]~data$alliance[data$countryname=="Germany"],title(main = "Germany"))
boxplot(data$distance[data$countryname=="Austria"]~data$alliance[data$countryname=="Austria"],title(main = "Austria"))
boxplot(data$distance[data$countryname=="Ireland"]~data$alliance[data$countryname=="Ireland"],title(main = "Ireland"))
boxplot(data$distance[data$countryname=="UK"]~data$alliance[data$countryname=="UK"],title(main = "UK"))
I think this could replace all your lines and fix your problem:
for (i in data$countryname)
boxplot(distance~alliance, subset(data, countryname==i), main=i)
But that's hard to verify without a reproducible example or some of your data.frame.
Based on the documentation, you should be assigning a title to your boxplots by making explicit calls to the function title(), rather than as a parameter in the call to boxplot(). The first two calls to generate your boxplots should look something like the following:
boxplot(data$distance[data$countryname=="Sweden"]~data$alliance[data$countryname=="Sweden"])
title(main = "Sweden")
boxplot(data$distance[data$countryname=="Norway"]~data$alliance[data$countryname=="Norway"])
title(main = "Norway")

Legend in multiple plot in R

According to the comments from others, this post has been separated into several
smaller questions from the previous version of this OP.
In the graph below, will you help me to (Newbie to R)
Custom legends according to the data they represent like filled for variable 1, circle points for variable 2 and line for variable 3 and their colors.
same letter size for the legend and axis-names.
The graph below is produced with the data in pdf device with following layout.
m <- matrix(c(1,2,3,3,4,5),nrow = 3,ncol = 2,byrow = TRUE)
layout(mat = m,heights = c(0.47,0.06,0.47))
par(mar=c(4,4.2,3,4.2))
#Codes for Fig A and B
...
#Margin for legend
par(mar = c(0.2,0.2,0.1,0.1))
# Code for legend
...
#Codes for Fig C and D
...
Using doubleYScale from latticeExtra and the data in the long format (see my previous answer), you can simplify the work:
No need to create a custom layout to superpose many plots
No need to create the legend manually
The idea is to create 2 separates objects and then merge them using doubleYScale. The latter will create the second axes. I hope I get your ploygon idea since it is not very clear why do you invert it in your OP.
library(latticeExtra)
obj1 <- xyplot(Variable~TimeVariable|Type,type='l',
groups=time, scales=list(x=list(relation='free'),
y=list(relation='free')),
auto.key=list(columns = 3,lines = TRUE,points=FALSE) ,
data = subset(dat.l,time !=1))
obj2 <- xyplot(Variable~TimeVariable|Type,
data = subset(dat.l,time ==1),type='l',
scales=list(x=list(alternating=2),
auto.key=list(columns = 3,lines = TRUE,points=FALSE),
y=list(relation='free')),
panel=function(x,y,...){
panel.xyplot(x,y,...)
panel.polygon(x,y,col='violetred4',border=NA,alpha=0.3)
})
doubleYScale(obj1, obj2, add.axis = TRUE,style1 = 0, style2 = 1)
Try the following:
1) For the legend part
The data can be found on https://www.dropbox.com/s/4kgq8tyvuvq22ym/stackfig1_2.csv
The code I used is as follows:
data <- read.csv("stackfig1_2.csv")
library(Hmisc)
label1=c(0,100,200,300)
plot(data$TimeVariable2C,data$Variable2C,axes=FALSE,ylab="",xlab="",xlim=c(0,24),
ylim=c(0,2.4),xaxs="i",yaxs="i",pch=19)
lines(data$TimeVariable3C,data$Variable3C)
axis(2,tick=T,at=seq(0.0,2.4,by=0.6),label= seq(0.0,2.4,by=0.6))
axis(1,tick=T,at=seq(0,24,by=6),label=seq(0,24,by=6))
mtext("(C)",side=1,outer=F,line=-10,adj=0.8)
minor.tick(nx=5,ny=5)
par(new=TRUE)
plot(data$TimeVariable1C,data$Variable1C,axes=FALSE,xlab="",ylab="",type="l",
ylim=c(800,0),xaxs="i",yaxs="i")
axis(3,xlim=c(0,24),tick=TRUE,at= seq(0,24,by=6),label=seq(0,24,by=6),col.axis="violetred4",col="violetred4")
axis(4,tick=TRUE,at= label1,label=label1,col.axis="violetred4",col="violetred4")
polygon(data$TimeVariable1C,data$Variable1C,col='violetred4',border=NA)
legend("top", legend = c("Variable A","Variable B","Variable C"), col = c("black","violetred4","black"),
ncol = 2, lwd =c("","",2),pch=c(19,15,NA),cex=1)
The output is as follows:
2) To make the font size same use the parameter cex and make it same everywhere.

Resources