在 r 中使用 geom_violin() 可视化矩阵
Visualize a matrix using geom_violin() in r
我有一个相关矩阵 (200x200),形式为:
>cormat
n1 n2 n3
n1 1.000000000 0.132555050 0.009169320
n2 -0.121419322 1.000000000 -0.174995204
n3 -0.259331076 -0.171652163 1.000000000
等等
我想使用 单小提琴图 可视化数据框中列之间相关性的分布,为此矩阵已创建。输入此代码后:
ggplot()+geom_violin(aes(c(cormat[1:200,]), c(cormat[,1:200])))
我得到了:
这是可能的结果吗?有没有更好的方法来使用 geom_violin() 绘制矩阵?
这将有助于使一些东西更具代表性:
library(ggplot2)
set.seed(69)
df <- data.frame(a = 1:10, b = 1/33 * 1:10 + rnorm(10), c = -(1:10) * 0.1 + rnorm(10),
d = 1/5 * 1:10 + rnorm(10), e = rnorm(10))
cormat <- cor(df)
现在在你的例子中,由于 cormat
是 500 平方,c(cormat[1:500,])
与 c(cormat[,1:500])
相同,它们都与 c(cormat)
相同,即只是 cormat
展开成一个 250,000 长度的向量。您的图实际上只是所有相关值的密度图。我不确定这有多大用处:
ggplot() + geom_violin(aes(c(cormat), c(cormat)))
您可以将所有相关性分别绘制为小提琴图:
plot_df <- reshape2::melt(cormat)
ggplot(data = plot_df) + geom_violin(aes(Var1, value, fill = Var1))
但这对 500 个变量不起作用。
表示这么大的相关矩阵的更标准方法是相关图,例如:
ggplot(plot_df) + geom_tile(aes(Var1, Var2, fill = value))
由 reprex package (v0.3.0)
于 2020-07-12 创建
我有一个相关矩阵 (200x200),形式为:
>cormat
n1 n2 n3
n1 1.000000000 0.132555050 0.009169320
n2 -0.121419322 1.000000000 -0.174995204
n3 -0.259331076 -0.171652163 1.000000000
等等
我想使用 单小提琴图 可视化数据框中列之间相关性的分布,为此矩阵已创建。输入此代码后:
ggplot()+geom_violin(aes(c(cormat[1:200,]), c(cormat[,1:200])))
我得到了:
这是可能的结果吗?有没有更好的方法来使用 geom_violin() 绘制矩阵?
这将有助于使一些东西更具代表性:
library(ggplot2)
set.seed(69)
df <- data.frame(a = 1:10, b = 1/33 * 1:10 + rnorm(10), c = -(1:10) * 0.1 + rnorm(10),
d = 1/5 * 1:10 + rnorm(10), e = rnorm(10))
cormat <- cor(df)
现在在你的例子中,由于 cormat
是 500 平方,c(cormat[1:500,])
与 c(cormat[,1:500])
相同,它们都与 c(cormat)
相同,即只是 cormat
展开成一个 250,000 长度的向量。您的图实际上只是所有相关值的密度图。我不确定这有多大用处:
ggplot() + geom_violin(aes(c(cormat), c(cormat)))
您可以将所有相关性分别绘制为小提琴图:
plot_df <- reshape2::melt(cormat)
ggplot(data = plot_df) + geom_violin(aes(Var1, value, fill = Var1))
但这对 500 个变量不起作用。
表示这么大的相关矩阵的更标准方法是相关图,例如:
ggplot(plot_df) + geom_tile(aes(Var1, Var2, fill = value))