gpt4 book ai didi

r - 如何使用 ggplot2 包在 R 中创建此 chart.Correlation 图?

转载 作者:行者123 更新时间:2023-12-04 10:39:38 26 4
gpt4 key购买 nike

我非常想使用纯 ggplot2 方法重新创建此图,因为我想使用该包解决所有绘图问题。

Plot that I would like to recreate using the ggplot2 package

生成此特定图的代码如下。

library(PerformanceAnalytics)
library(quantmod)
getSymbols(c('SPY','LQD','GLD'))
Data <- cbind(Cl(SPY),Cl(LQD),Cl(GLD))
Year <- as.factor(format(index(Data),'%Y'))
chart.Correlation(Data,bg=seq(1:5)[Year],pch=21)
par(xpd=TRUE)
legend(0, 1, as.vector(unique(Year)), fill=seq(1:5))

您可以在 this site 上阅读更多相关信息.

我创建了一个小数据集来玩。下面的代码定义了一个data.frame并将其熔化成ggplot通常喜欢的格式。

library(ggplot2)
library(reshape2)
mydf<-structure(list(SPY = c(141.37, 141.67, 140.54, 141.19, 141.07,
141.54, 142.16, 143.24, 142.96, 143.02, 142.54, 142.82, 142.38,
142.8, 143.95, 142.26, 142.13, 142.05, 142.79, 143.75, 144.86,
141.31, 141.19, 138.91, 140.37, 141.29, 140.15, 141.28, 138.17,
136.98, 133.43, 132.06, 130.72, 133.86, 134.99, 133.04, 135.24,
135.91, 134.91, 137.37, 92.96, 92.85, 93.47, 90.67, 91.04, 89.09,
86.95, 87.11, 84.37, 84.4, 85.06, 80.57, 84.05, 82.75, 83.11,
83.68, 84.53, 87.39, 84.55, 82.83),
LQD = c(106.45, 107.25, 107.19, 107.15, 107.17, 107.03, 106.7, 106.59,
106.71, 106.56, 106.87,
106.81, 106.88, 106.59, 106.64, 106.3, 106.23, 106.18, 106.33,
106.78, 105.73, 106.6, 106.47, 106.27, 106.05, 105.74, 106.51,
106.07, 106.64, 106.48, 106.98, 106.8, 107.03, 107.17, 106.26,
107.41, 107.38, 107.2, 107.18, 107.53, 100.61, 101.3, 101.47,
100.62, 101.34, 102.6, 102.07, 101.85, 101.4, 101.6, 101.6, 100.1,
99.6, 99.23, 98.57, 97.69, 99.53, 100.32, 99.12, 99.82),
GLD = c(62.28, 61.65, 60.17, 60.48, 60.85, 60.59, 60.63, 62.17, 61.97, 62.64,
62.26, 63, 62.72, 64.23, 64.33, 64.06, 64.17, 63.77, 64.22, 64.83,
85.57, 85.13, 84.77, 86.78, 86.55, 88.25, 88.58, 89.54, 87.99,
86.7, 86.5, 87.42, 88.17, 87.89, 90.08, 90.3, 91.75, 91.15, 92.06,
91.4, 86.23, 84.48, 85.13, 82.75, 84.46, 83.92, 80.76, 80.88,
79.79, 80.39, 82.71, 84.52, 84.15, 84.58, 88.53, 88.95, 88.38,
87.42, 89.5, 91.31),
Year = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L),
.Label = c("2007", "2008", "2009"),
class = "factor")),
.Names = c("SPY", "LQD", "GLD", "Year"),
row.names = c("2007-01-02", "2007-01-03",
"2007-01-04", "2007-01-07", "2007-01-08", "2007-01-09", "2007-01-10",
"2007-01-11", "2007-01-15", "2007-01-16", "2007-01-17", "2007-01-18",
"2007-01-21", "2007-01-22", "2007-01-23", "2007-01-24", "2007-01-25",
"2007-01-28", "2007-01-29", "2007-01-30", "2008-01-02", "2008-01-03",
"2008-01-06", "2008-01-07", "2008-01-08", "2008-01-09", "2008-01-10",
"2008-01-13", "2008-01-14", "2008-01-15", "2008-01-16", "2008-01-17",
"2008-01-21", "2008-01-22", "2008-01-23", "2008-01-24", "2008-01-27",
"2008-01-28", "2008-01-29", "2008-01-30", "2009-01-01", "2009-01-04",
"2009-01-05", "2009-01-06", "2009-01-07", "2009-01-08", "2009-01-11",
"2009-01-12", "2009-01-13", "2009-01-14", "2009-01-15", "2009-01-19",
"2009-01-20", "2009-01-21", "2009-01-22", "2009-01-25", "2009-01-26",
"2009-01-27", "2009-01-28", "2009-01-29"),
class = "data.frame")
mydf<-data.frame(ID=rownames(mydf), mydf)
moltdf<-melt(mydf)

现在我知道这个 moltdf 可以传递给 ggplot,但我不知道如何在上面信息密集的图中生成不同的片段。

最佳答案

我认为您在这里需要 ggpairs - 可能需要进一步调整。示例:

> library(GGally)
> ggpairs(mtcars)

simple ggpairs demo


更新:基于添加数据的更新问题:

> ggpairs(mydf[, 2:5])

ggpairs on mydf

关于r - 如何使用 ggplot2 包在 R 中创建此 chart.Correlation 图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13798399/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com