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")
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!
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")
I coulnd't found any post with a related subject. I actually don't know if its posible.
So I have my. csv file:
Periodo;Teorico;Real;F1;F2;F3
20140101;50;20;7;7;16
20140108;55;29;11;5;5
20140115;52;21,4;8,6;10;12
20140122;66;32;9;8;17
I asign it to a data.frame:
df<-read.csv2('d:\\xxx\\test2.csv', header = T, sep = ";")
Then I do barplot function:
bp <- barplot(t(df[,-c(1:2)]),col=c("blue", "red", "green", "yellow"),legend=colnames(df[,-c(1:2)]),args.legend = list(x="topleft"))
axis(side = 1, at = bp, labels = df$Periodo)
title(main = "Teorico = Real + F1+F2+F3", font.main = 4)
Now I must calculate the following function: (efficiency function)
((Teorico-Real)/Teorico)*100
And represent the result of the function of each row on the top of each Periodo (week).
If you could help me with the code for the function and "replotting" parts or give some guidelines or posts related to this I would be really gratefull.
Thanks
You can try:
lbls <- round(((df$Teorico - df$Real) / df$Teorico)* 100)
mtext(lbls, at=bp)
(I just used round to make it look better.)
I have a data set that I've successfully read into R. It's a simple data.frame with ONE ROW of data (I'm not sure how many columns, but its in the hundreds). It was read with column headers, but no row labels. So the data set looks something like this:
df=structure(list(X500000 = 0.0958904109589041, X1500000 = 0.10958904109589, X2500000 = 0.10958904109589, X3500000 = 0.164383561643836, X4500000 = 0.136986301369863, X5500000 = 0.205479452054795, X6500000 = 0.136986301369863, X7500000 = 0.0273972602739726, X8500000 = 0.0821917808219178, X9500000 = 0.178082191780822), .Names = c("X500000", "X1500000", "X2500000", "X3500000", "X4500000", "X5500000", "X6500000", "X7500000", "X8500000", "X9500000"), class = "data.frame", row.names = 79L)
Except that it is MUCH LARGER (I don't know if it matters, but it has around 300 columns going across). I'm trying to plot it so that the X##### labels are on the x axis, and the value of each data point is plotted on the y axis (say like a scatter plot on excel or even a line graph). Doing just plot(df) gives me an extremely bizarre graph that makes no sense to me (a bunch of boxes each with a dot right in the centre and no labels?).
I have a feeling it might work if I were to transform the data frame into a vector by removing the headings and then adding x-axis labels individually afterwards and doing a plot() on the vector, but if there is a way of avoiding that it would be great....
As explained in '?plot', 'x' and 'y' must be two vectors of numerics, of same size:
df=structure(list(X500000 = 0.0958904109589041, X1500000 = 0.10958904109589, X2500000 = 0.10958904109589, X3500000 = 0.164383561643836, X4500000 = 0.136986301369863, X5500000 = 0.205479452054795, X6500000 = 0.136986301369863, X7500000 = 0.0273972602739726, X8500000 = 0.0821917808219178, X9500000 = 0.178082191780822), .Names = c("X500000", "X1500000", "X2500000", "X3500000", "X4500000", "X5500000", "X6500000", "X7500000", "X8500000", "X9500000"), class = "data.frame", row.names = 79L)
plot(x=as.numeric(substr(names(df),2,nchar(names(df)))), as.numeric(df), xlab="This is xlab", ylab="This is y")