2017-07-14 165 views
3

PerformanceAnalytics下面的腳本圖2圖並排:並排地塊爲R中

require(xts) 
par(mfrow=c(1,2)) 
XTS1 <- structure(c(12, 7, 7, 22, 24, 30, 26, 23, 27, 30), .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), tzone = "", tclass = c("POSIXct", "POSIXt")), .Dim = c(10L, 1L)) 
XTS2 <- XTS1^0.2 
plot(XTS1) 
plot(XTS2) 

下面的腳本未能並排繪製兩個圖表方:

require(PerformanceAnalytics) 
require(xts) 
par(mfrow=c(1,2)) 
XTS1 <- structure(c(12, 7, 7, 22, 24, 30, 26, 23, 27, 30), .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), tzone = "", tclass = c("POSIXct", "POSIXt")), .Dim = c(10L, 1L)) 
XTS2 <- XTS1^0.2 
charts.PerformanceSummary(XTS1) 
charts.PerformanceSummary(XTS2) 

會有人知道如何讓後面的腳本並排繪製2個圖表?

如果可能,我想避免使用其他軟件包。謝謝。

+2

看起來像'charts.PerformanceSummary'內部使用'layout'來設置它的三個圖。我認爲最好的選擇是將兩張圖並排存入圖中,然後將它們放在一起。 – Gregor

回答

3

chart.PerformanceSummary實際上只是幾個圖表的包裝。

你可以做到這一點,如果你想(超過2個符號,如果你想)enter image description here它橫向擴展到任何數量的符號:

par(mfrow=c(3,2)) 
# First row 
chart.CumReturns(XTS1, ylab = "Cumulative Return", main = "give me a title") 
chart.CumReturns(XTS2, ylab = "Cumulative Return", main = "give me a title2") 
# second row 
chart.BarVaR(XTS1) 
chart.BarVaR(XTS2) 

# third row 
chart.Drawdown(XTS1, main = "DD title", ylab = "Drawdown", 
) 
chart.Drawdown(XTS2, main = "", ylab = "Drawdown", 
) 

您需要將相應的參數添加到每個小區的比如顏色和標題(把它留給你),但是你可以靈活地添加美妙的xts,quantmod,performanceAnalytics包(和其他)的任何圖表。