The code below uses plot.zoo function to plot two variables (in xts format) on the same panel with different colors. I need to generate a legend for variables, showing variable name and associated color (to distinguish one from the other). I could not find any examples. How can this be achieved?
library(xts)
XTS1 <- structure(c(0.228369333217119, 0.228289904084397, 0.228447230658951, 0.228791576808238, 0.230747528582736, 0.231930951391005, 0.23218095279688, 0.232655671733178, 0.232780332790686, 0.232737419368931, 0.232662430253128, 0.232742568778743, 0.233149068581523, 0.233801848401534, 0.233772492034511, 0.234624948258082, 0.23483556890299, 0.234796321794256, 0.23604671397228, 0.236133475815726, 0.236165665606426, 0.236028593613328, 0.23653404809854, 0.236405363926972, 0.236289501951192, 0.23595748354442, 0.235856148573546, 0.236430492268466, 0.236509873541663, 0.236320952959145, 0.236324663117125, 0.236230849130264, 0.236256142711785, 0.236443262637705, 0.236474635240435, 0.236478152698594, 0.236446010557742, 0.236433585738065, 0.236318823709326, 0.236343709733496, 0.236353484497234, 0.236293047785645, 0.236299398621113, 0.23632602130195, 0.236415592036867, 0.23644183016732, 0.236410860513062, 0.236407738860613, 0.236488620207848, 0.236521885178186, 0.236524525976254, 0.236543261190294, 0.236545852954505, 0.236554043549182, 0.236558510962661, 0.236500857781485, 0.236456811183667, 0.236429475415132, 0.23640238691203, 0.236406136067832, 0.236417470442709, 0.236338677460503, 0.236460106937516, 0.236499259900878, 0.236586474063617, 0.236543271644404, 0.236513672740977, 0.236369639034221, 0.236362697687712, 0.236797843111073, 0.237345016578261, 0.23783668813706, 0.237923216897962, 0.238248672738757, 0.23827277432708, 0.238353796658431, 0.238432929684165, 0.238426329912625, 0.238345183139418, 0.238458037645863, 0.238479196815153, 0.238658887053061, 0.238465939669501, 0.238523649852224, 0.238510811967291, 0.238618072611544, 0.238704138338676, 0.238707447338575, 0.23996516914318, 0.239424589962875, 0.239561518553478, 0.239421256904757, 0.239816852104079, 0.239445276547969, 0.239609107802299, 0.240012364436436, 0.24002458654273, 0.240130590848021, 0.239736081751086, 0.239857898122929, 0.249230991607954, 0.239805445609213, 0.220333558462471, 0.210410600760776, 0.210378078591145, 0.240399004222477, 0.239804814201628, 0.240033060931268, 0.240089397482534, 0.240197041264942, 0.239940687229403, 0.239983219836939, 0.240022917769706, 0.240337756345468, 0.240638638953238, 0.240145924499555, 0.240402820873626, 0.240634154733532, 0.240611140050359, 0.240534865617682, 0.240951950137048, 0.241127845406939, 0.241125700147753, 0.241126235785769, 0.24107570794763, 0.241088551839332, 0.241092988182315, 0.241086472173767, 0.241083390403282, 0.241091888348645, 0.241083158087253, 0.241064751836135, 0.241064210485496, 0.241091022867438, 0.240953810187424, 0.240481188224338, 0.240621074487305, 0.240541023392083, 0.240396295416436, 0.240434646218793, 0.240610526667198, 0.240808207542551, 0.240631718256586, 0.24064699063015, 0.24068954416015, 0.240711128194114, 0.240723728654195, 0.240775499747258, 0.24076580240029, 0.240772143433637, 0.240793451595961, 0.240796153528682, 0.240819395590463, 0.240807928484687, 0.241070198676456, 0.241015031342511, 0.240992277437459, 0.241067975400449, 0.241065024303712, 0.241163875314065, 0.241210015023487, 0.24111104460957, 0.241143389170502, 0.241126406233165, 0.241236385470386, 0.241216017627354, 0.241242224901225, 0.241204855537627, 0.241161113202215, 0.241188009018793, 0.241155655393916, 0.241104796660299, 0.241229168370518, 0.241187912977691, 0.241205571324508, 0.241275488693839, 0.241201890756208, 0.241062150030789, 0.241056667454678, 0.241020382598737, 0.241011174472952, 0.241075153997628, 0.240860609085096, 0.241671867834864, 0.242222875244792, 0.242105277990476, 0.242789197437053, 0.24262651942461, 0.242835209836078, 0.242065722773181, 0.242128191776504, 0.241866750527435, 0.242222755781399, 0.242405920724485, 0.242704642796114, 0.243681731065406, 0.243823985845211, 0.244075416453679, 0.244168166839201), .indexCLASS = c("POSIXct", "POSIXt"), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "", class = c("xts", "zoo"), .CLASS = structure("double", class = "CLASS"), formattable = structure(list(
formatter = "formatC", format = structure(list(format = "f", digits = 2), .Names = c("format", "digits")), preproc = "percent_preproc", postproc = "percent_postproc"), .Names = c("formatter", "format", "preproc", "postproc")), index = structure(c(1413981900, 1413982800, 1413983700, 1413984600, 1413985500, 1413986400, 1413987300, 1413988200, 1413989100, 1413990000, 1413990900, 1413991800, 1413992700, 1413993600, 1413994500, 1413995400, 1413996300, 1413997200, 1413998100, 1413999000, 1413999900, 1414000800, 1414001700, 1414002600, 1414003500, 1414004400, 1414005300, 1414006200, 1414007100, 1414008000, 1414009800, 1414010700, 1414011600, 1414015200, 1414016100, 1414017000, 1414017900, 1414018800, 1414019700, 1414020600, 1414021500, 1414022400, 1414023300, 1414024200, 1414025100, 1414026000, 1414026900, 1414027800, 1414028700, 1414029600, 1414030500, 1414031400, 1414032300, 1414033200, 1414034100, 1414035000, 1414035900, 1414036800, 1414037700, 1414038600, 1414039500, 1414040400, 1414041300, 1414042200, 1414043100, 1414044000, 1414044900, 1414045800, 1414046700, 1414047600, 1414048500, 1414049400, 1414050300, 1414051200, 1414052100, 1414053000, 1414053900, 1414054800, 1414055700, 1414056600, 1414057500, 1414058400, 1414059300, 1414060200, 1414061100, 1414062000, 1414062900, 1414063800, 1414064700, 1414065600, 1414066500, 1414067400, 1414068300, 1414069200, 1414070100, 1414071000, 1414071900, 1414072800, 1414073700, 1414074600, 1414075500, 1414076400, 1414077300, 1414078200, 1414079100, 1414080000, 1414080900, 1414081800, 1414082700, 1414083600, 1414084500, 1414085400, 1414086300, 1414087200, 1414088100, 1414089000, 1414089900, 1414090800, 1414091700, 1414092600, 1414093500, 1414094400, 1414096200, 1414097100, 1414098000, 1414101600, 1414102500, 1414103400, 1414104300, 1414105200, 1414106100, 1414107000, 1414107900, 1414108800, 1414109700, 1414110600, 1414111500, 1414112400, 1414113300, 1414114200, 1414115100, 1414116000, 1414116900, 1414117800, 1414118700, 1414119600, 1414120500, 1414121400, 1414122300, 1414123200, 1414124100, 1414125000, 1414125900, 1414126800, 1414127700, 1414128600, 1414129500, 1414130400, 1414131300, 1414132200, 1414133100, 1414134000, 1414134900, 1414135800, 1414136700, 1414137600, 1414138500, 1414139400, 1414140300, 1414141200, 1414142100, 1414143000, 1414143900, 1414144800, 1414145700, 1414146600, 1414147500, 1414148400, 1414149300, 1414150200, 1414151100, 1414152000, 1414152900, 1414153800, 1414154700, 1414155600, 1414156500, 1414157400, 1414158300, 1414159200, 1414160100, 1414161000, 1414161900, 1414162800, 1414163700, 1414164600, 1414165500, 1414166400, 1414167300), tzone = "", tclass = c("POSIXct", "POSIXt")), .Dim = c(199L, 1L))
XTS2 <- structure(c(0.238369333217119, 0.238289904084397, 0.238447230658951, 0.238791576808238, 0.230747528582736, 0.231930951391005, 0.23218095279688, 0.232655671733178, 0.232780332790686, 0.232737419368931, 0.232662430253128, 0.232742568778743, 0.233149068581523, 0.233801848401534, 0.233772492034511, 0.234624948258082, 0.23483556890299, 0.234796321794256, 0.23604671397238, 0.236133475815726, 0.236165665606426, 0.236028593613328, 0.23653404809854, 0.236405363926972, 0.236289501951192, 0.23595748354442, 0.235856148573546, 0.236430492368466, 0.236509873541663, 0.236320952959145, 0.236324663117125, 0.236230849130264, 0.236256142711785, 0.236443262637705, 0.236474635240435, 0.236478152698594, 0.236446010557742, 0.236433585738065, 0.236318823709326, 0.236343709733496, 0.236353484497234, 0.236293047785645, 0.236299398621113, 0.23632602130195, 0.236415592036867, 0.23644183016732, 0.236410860513062, 0.236407738860613, 0.236488620207848, 0.236521885178186, 0.236524525976254, 0.236543261190294, 0.236545852954505, 0.236554043549182, 0.236558510962661, 0.236500857781485, 0.236456811183667, 0.236429475415132, 0.23640238691203, 0.236406136067832, 0.236417470442709, 0.236338677460503, 0.236460106937516, 0.236499259900878, 0.236586474063617, 0.236543271644404, 0.216513672740977, 0.216369639034221, 0.216362697687712, 0.216797843111073, 0.217345016578261, 0.21783668813706, 0.217921216897962, 0.218248672738757, 0.21827277432708, 0.218353796658431, 0.218432929684165, 0.218426329912625, 0.218345183139418, 0.218458037645863, 0.218479196815153, 0.238658887053061, 0.238465939669501, 0.238523649852224, 0.238510811967291, 0.238618072611544, 0.238704138338676, 0.238707447338575, 0.23996516914318, 0.239424589962875, 0.239561518553478, 0.239421256904757, 0.239816852104079, 0.239445276547969, 0.239609107802299, 0.240012364436436, 0.24002458654273, 0.240130590848021, 0.239736081751086, 0.239857898122929, 0.239230991607954, 0.239805445609213, 0.240333558462471, 0.240410600760776, 0.240378078591145, 0.240399004222477, 0.239804814201628, 0.240033060931268, 0.240089397482534, 0.240197041264942, 0.239940687229403, 0.239983219836939, 0.240022917769706, 0.240337756345468, 0.240638638953238, 0.240145924499555, 0.240402820873626, 0.240634154733532, 0.240611140050359, 0.240534865617682, 0.240951950137048, 0.241127845406939, 0.241125700147753, 0.241126235785769, 0.24107570794763, 0.241088551839332, 0.241092988182315, 0.241086472173767, 0.241083390403282, 0.241091888348645, 0.241083158087253, 0.241064751836135, 0.241064210485496, 0.241091022867438, 0.240953810187424, 0.240481188224338, 0.240621074487305, 0.240541023392083, 0.240396295416436, 0.240434646218793, 0.240610526667198, 0.240808207542551, 0.240631718256586, 0.24064699063015, 0.24068954416015, 0.240711128194114, 0.240723728654195, 0.240775499747258, 0.24076580240029, 0.240772143433637, 0.240793451595961, 0.240796153528682, 0.240819395590463, 0.240807928484687, 0.241070198676456, 0.241015031342511, 0.240992277437459, 0.241067975400449, 0.241065024303712, 0.241163875314065, 0.241210015023487, 0.24111104460957, 0.241143389170502, 0.241126406233165, 0.241236385470386, 0.241216017627354, 0.241242224901225, 0.241204855537627, 0.241161113202215, 0.241188009018793, 0.241155655393916, 0.241104796660299, 0.241229168370518, 0.241187912977691, 0.241205571324508, 0.241275488693839, 0.241201890756208, 0.241062150030789, 0.241056667454678, 0.241020382598737, 0.241011174472952, 0.241075153997628, 0.240860609085096, 0.241671867834864, 0.242222875244792, 0.242105277990476, 0.242789197437053, 0.24262651942461, 0.242835209836078, 0.242065722773181, 0.242128191776504, 0.241866750527435, 0.242222755781399, 0.242405920724485, 0.242704642796114, 0.243681731065406, 0.243823985845211, 0.244075416453679, 0.244168166839201), .indexCLASS = c("POSIXct", "POSIXt"), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "", class = c("xts", "zoo"), .CLASS = structure("double", class = "CLASS"), formattable = structure(list(
formatter = "formatC", format = structure(list(format = "f", digits = 2), .Names = c("format", "digits")), preproc = "percent_preproc", postproc = "percent_postproc"), .Names = c("formatter", "format", "preproc", "postproc")), index = structure(c(1413981900, 1413982800, 1413983700, 1413984600, 1413985500, 1413986400, 1413987300, 1413988200, 1413989100, 1413990000, 1413990900, 1413991800, 1413992700, 1413993600, 1413994500, 1413995400, 1413996300, 1413997200, 1413998100, 1413999000, 1413999900, 1414000800, 1414001700, 1414002600, 1414003500, 1414004400, 1414005300, 1414006200, 1414007100, 1414008000, 1414009800, 1414010700, 1414011600, 1414015200, 1414016100, 1414017000, 1414017900, 1414018800, 1414019700, 1414020600, 1414021500, 1414022400, 1414023300, 1414024200, 1414025100, 1414026000, 1414026900, 1414027800, 1414028700, 1414029600, 1414030500, 1414031400, 1414032300, 1414033200, 1414034100, 1414035000, 1414035900, 1414036800, 1414037700, 1414038600, 1414039500, 1414040400, 1414041300, 1414042200, 1414043100, 1414044000, 1414044900, 1414045800, 1414046700, 1414047600, 1414048500, 1414049400, 1414050300, 1414051200, 1414052100, 1414053000, 1414053900, 1414054800, 1414055700, 1414056600, 1414057500, 1414058400, 1414059300, 1414060200, 1414061100, 1414062000, 1414062900, 1414063800, 1414064700, 1414065600, 1414066500, 1414067400, 1414068300, 1414069200, 1414070100, 1414071000, 1414071900, 1414072800, 1414073700, 1414074600, 1414075500, 1414076400, 1414077300, 1414078200, 1414079100, 1414080000, 1414080900, 1414081800, 1414082700, 1414083600, 1414084500, 1414085400, 1414086300, 1414087200, 1414088100, 1414089000, 1414089900, 1414090800, 1414091700, 1414092600, 1414093500, 1414094400, 1414096200, 1414097100, 1414098000, 1414101600, 1414102500, 1414103400, 1414104300, 1414105200, 1414106100, 1414107000, 1414107900, 1414108800, 1414109700, 1414110600, 1414111500, 1414112400, 1414113300, 1414114200, 1414115100, 1414116000, 1414116900, 1414117800, 1414118700, 1414119600, 1414120500, 1414121400, 1414122300, 1414123200, 1414124100, 1414125000, 1414125900, 1414126800, 1414127700, 1414128600, 1414129500, 1414130400, 1414131300, 1414132200, 1414133100, 1414134000, 1414134900, 1414135800, 1414136700, 1414137600, 1414138500, 1414139400, 1414140300, 1414141200, 1414142100, 1414143000, 1414143900, 1414144800, 1414145700, 1414146600, 1414147500, 1414148400, 1414149300, 1414150200, 1414151100, 1414152000, 1414152900, 1414153800, 1414154700, 1414155600, 1414156500, 1414157400, 1414158300, 1414159200, 1414160100, 1414161000, 1414161900, 1414162800, 1414163700, 1414164600, 1414165500, 1414166400, 1414167300), tzone = "", tclass = c("POSIXct", "POSIXt")), .Dim = c(199L, 1L))
z <- cbind(XTS1, XTS2)
colnames(z) <- c("XTS1", "XTS2")
plot.zoo(z, plot.type = "single", col = 1:2, xy.labels = "text")
Please note I need to use plot.zoo rather than plot.
Solution was provided in one of the comments. Here's full solution plus some explanatory notes:
z <- cbind(XTS1, XTS2)
colnames(z) <- c("XTS1", "XTS2")
plot.zoo(z, plot.type = "single", col = 1:2)
legend("topright", inset=c(0,0), y.intersp = 1, legend = c("XTS1", "XTS2"), lty = 1, bty = "n", col = c(1,2), cex = .5)
#cex: font size
#col: color
#bty: box around legends
#pch: symbol for legends (eg pch = c(1,2) => circle, triangle)
#lty: lines for legends (eg: lty = c(1,2) => solid, dash)
#y.intersp: vert interspacing (wout it legends may be far apart from each other)
#inset: for adjusting pos relative to eg "topright" (useful for xts as x coord in posixct format)
I'm trying to format a chart using GGPLOT2. What I'm trying to accomplish feels like it should be fairly straightforward, but try as I might, no dice. I have the following chart.
#packages
library(ggplot2)
library(plyr)
library(reshape2)
library(PerformanceAnalytics)
library(timeSeries)
library(quantmod)
library(ggthemes)
#stock data
getSymbols(c("^FCHI","^GSPC"), from = "2008-12-31")
stockmarketdata <- cbind(GSPC$GSPC.Close, FCHI$FCHI.Close)
#normalize data
stockmarketdata$CAC40 <- stockmarketdata[,2] / 3217.97
stockmarketdata$SNP <- stockmarketdata[ ,1] / 903.25
#Isolate normalized data
marketdata <- stockmarketdata[,3:4]
GSPC.DF<-data.frame(Date=index(GSPC),coredata(GSPC))
FCHI.DF<-data.frame(Date=index(FCHI),coredata(FCHI))
#format before making ggplot chart
market.df <- data.frame(Date=index(marketdata), marketdata)
market.df.eco <- market.df
colnames(market.df.eco) <- c("Date", "CAC40", "S&P500")
market.df.eco.mlt <- melt(market.df.eco, id = "Date")
ggplot chart
chart.EQ <-
ggplot(market.df.eco.mlt, aes(x=Date, y=value, colour = variable, group = variable)) +
geom_line() +
labs(title="Equity Market", x= "", y = "", color="December 31st 2008=1", title.vjust=1) +
theme_economist() +
scale_color_economist() +
theme(legend.position = c(0,1),
legend.justification=c(0,1),
legend.direction="horizontal",
plot.title = element_text(vjust=1),
legend.title=element_text(vjust=1),
legend.title.align=0)
which looks like this
Ideally, I would like the title and legend to be flush with the left side. I would like both the legend and the title to be above the chart itself with the legend title directly below the chart title and the labels in the legend below the legend title. More like this.
If someone could help me with this formatting that would be excellent. Whenever I remove the justification argument from the theme(), the legend flies half way off the image. Thanks for any help you can offer!
If you don't have the finance packages here is a small sample of the data frame 'marketdata' if you apply the transformations from the code above you should be able to get a similar chart
marketdata<-
structure(c(0.999999990988107, 1.04093261932212, 1.04411163621786,
1.05539205492904, 1.03981394730218, 1.03305191720246, 1.02533584837646,
1.00874778726961, 0.993760008017477, 0.948424006438842, 0.930984404142985,
0.937469895617423, 0.929060849231037, 0.909045152378674, 0.902920185085629,
0.891748561049357, 0.885384230741741, 0.918395795175219, 0.918134733698574,
0.955885235101632, 0.935294611198986, 0.92416023828687, 0.910527459547479,
0.926792323421287, 0.953703729369758, 0.952864706321066, 0.970422359127027,
0.9741763027623, 0.938712915285102, 0.94087886804414, 0.921183257768096,
0.931599768487587, 0.920524420985901, 0.893491853559853, 0.893131405202659,
0.892674604797434, 0.854746951960397, 0.847699051575994, 0.841539867991311,
0.838081126300121, 1, 1.03160806864102, 1.02679215278162, 1.03481872349848,
1.00376421145862, 1.0071740714088, 0.985718213119291, 0.963476346526432,
0.965170194298367, 0.932875721007473, 0.934115682258511, 0.94117907002491,
NA, 0.891469660669803, 0.930240786050374, 0.916136174923886,
0.92106284195959, 0.926177699418766, 0.936296730694714, 0.967716608912261,
0.935665668419596, 0.914342657071686, 0.913855523941323, 0.928325502352615,
0.921372798228619, 0.936451675615832, 0.961638500968724, 0.963066720177138,
0.915759726543039, 0.923044550235262, 0.924649877663991, 0.915405510102408,
NA, 0.873700507057847, 0.872870172156103, 0.862374760033213,
0.85253250816496, 0.82295047550512, 0.8559535178522, 0.846830915029062
), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC", src = "yahoo", updated = structure(1440080950.2661, class = c("POSIXct",
"POSIXt")), class = c("xts", "zoo"), index = structure(c(1230681600,
1230854400, 1231113600, 1231200000, 1231286400, 1231372800, 1231459200,
1231718400, 1231804800, 1231891200, 1231977600, 1232064000, 1232323200,
1232409600, 1232496000, 1232582400, 1232668800, 1232928000, 1233014400,
1233100800, 1233187200, 1233273600, 1233532800, 1233619200, 1233705600,
1233792000, 1233878400, 1234137600, 1234224000, 1234310400, 1234396800,
1234483200, 1234742400, 1234828800, 1234915200, 1235001600, 1235088000,
1235347200, 1235433600, 1235520000), tzone = "UTC", tclass = "Date"), .Dim = c(40L,
2L), .Dimnames = list(NULL, c("CAC40", "SNP")))