存储在 3D 数组中的对象的 ggplot
ggplot for objects stored in an 3D array
我已经编写了以下代码,可以正常工作并绘制我想要的内容,但是如果我想使用 ggplot 进行绘制,我该怎么做呢?
myseq<-seq(from = 1, to = 99, by = 5)
mtx <- array(rnorm(880,0,1) ,c(4,11,length(myseq)))
plot(myseq,mtx[1,1,],type = "l", col=1)
lines(myseq,mtx[1,2,],type = "l",col=2)
lines(myseq,mtx[1,3,],type = "l",col=3)
lines(myseq,mtx[1,4,],type = "l",col=4)
lines(myseq,mtx[1,5,],type = "l",col=5)
lines(myseq,mtx[1,6,],type = "l",col=6)
lines(myseq,mtx[1,7,],type = "l",col=7)
lines(myseq,mtx[1,8,],type = "l",col=8)
lines(myseq,mtx[1,9,],type = "l",col=9)
lines(myseq,mtx[1,10,],type = "l",col=10)
lines(myseq,mtx[1,11,],type = "l",col=11)
在 运行 之后我得到了如下图,现在如何使用 ggplot2 来做这件事?
enter image description here
ggplot2
更喜欢数据帧,对于像这样的东西,长格式的帧。
这是一个基本的方法。
首先,可重现的随机数据。
set.seed(42)
myseq<-seq(from = 1, to = 99, by = 5)
mtx <- array(rnorm(880,0,1) ,c(4,11,length(myseq)))
mtx[1,1:4,1:4]
# [,1] [,2] [,3] [,4]
# [1,] 1.3709584 -1.3682810 0.9333463 6.288407e-05
# [2,] 0.4042683 -0.4314462 0.6503486 -1.173196e-01
# [3,] 2.0184237 1.5757275 -1.1317387 -8.610730e-02
# [4,] -1.3888607 0.6792888 1.2009654 -4.138688e-01
从这里,我们基本上可以将这个三维数组转换成四列data.frame
,其中三列表示坐标轴,第四列是实际值。
melted_mtx <- reshape2::melt(mtx[1,,,drop=FALSE])
str(melted_mtx, vec.len=15)
# 'data.frame': 220 obs. of 4 variables:
# $ Var1 : int 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
# $ Var2 : int 1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 ...
# $ Var3 : int 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 ...
# $ value: num 1.371 0.404 2.018 -1.389 -0.284 -0.307 1.895 0.46 1.035 -0.784 0.206 -1.368 -0.431 1.576 0.679 -0.367 -0.727 0.921 0.624 ...
因为您想在 x-axis 上绘制 myseq
,我们可以用它来替换。它在此处的第三个变量中,Var3
。我会验证它的长度是否正确,然后进行替换:
lapply(melted_mtx[-4], table)
# $Var1
# 1
# 220
# $Var2
# 1 2 3 4 5 6 7 8 9 10 11
# 20 20 20 20 20 20 20 20 20 20 20
# $Var3
# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11
melted_mtx$Var3 <- myseq[melted_mtx$Var3]
lapply(melted_mtx[-4], table)
# $Var1
# 1
# 220
# $Var2
# 1 2 3 4 5 6 7 8 9 10 11
# 20 20 20 20 20 20 20 20 20 20 20
# $Var3
# 1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96
# 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11
我们可以使用它并将其直接提供给 ggplot2
。
library(ggplot2)
ggplot(melted_mtx, aes(Var3, value, group = Var2, color = factor(Var2))) +
geom_line()
(您的代码)呈现的基本图形图:
撇开边距和颜色不谈,实际上是同一个图。可以做很多美化,包括图例标签、坐标轴等
上面,我只melt
编辑了数组的第一个平面。如果你做了整个数组,那么它看起来像这样:
melted_mtx <- reshape2::melt(mtx)
melted_mtx$Var3 <- myseq[melted_mtx$Var3]
ggplot(melted_mtx, aes(Var3, value, group = interaction(Var1, Var2),
color = interaction(Var1, Var2))) +
geom_line()
这个有点复杂,很明显,但是使用interaction
,可以按多个变量分组。这里需要分组,因为没有它 ggplot2
将尝试将所有点连接在一条线上,通常没有用。通常可以使用 just color=
来建议组,但我经常包括 group=
和 color=
以防我以后改变方式colors/linetypes/shapes/... 已定义,组被意外更改。
我已经编写了以下代码,可以正常工作并绘制我想要的内容,但是如果我想使用 ggplot 进行绘制,我该怎么做呢?
myseq<-seq(from = 1, to = 99, by = 5)
mtx <- array(rnorm(880,0,1) ,c(4,11,length(myseq)))
plot(myseq,mtx[1,1,],type = "l", col=1)
lines(myseq,mtx[1,2,],type = "l",col=2)
lines(myseq,mtx[1,3,],type = "l",col=3)
lines(myseq,mtx[1,4,],type = "l",col=4)
lines(myseq,mtx[1,5,],type = "l",col=5)
lines(myseq,mtx[1,6,],type = "l",col=6)
lines(myseq,mtx[1,7,],type = "l",col=7)
lines(myseq,mtx[1,8,],type = "l",col=8)
lines(myseq,mtx[1,9,],type = "l",col=9)
lines(myseq,mtx[1,10,],type = "l",col=10)
lines(myseq,mtx[1,11,],type = "l",col=11)
在 运行 之后我得到了如下图,现在如何使用 ggplot2 来做这件事?
enter image description here
ggplot2
更喜欢数据帧,对于像这样的东西,长格式的帧。
这是一个基本的方法。
首先,可重现的随机数据。
set.seed(42)
myseq<-seq(from = 1, to = 99, by = 5)
mtx <- array(rnorm(880,0,1) ,c(4,11,length(myseq)))
mtx[1,1:4,1:4]
# [,1] [,2] [,3] [,4]
# [1,] 1.3709584 -1.3682810 0.9333463 6.288407e-05
# [2,] 0.4042683 -0.4314462 0.6503486 -1.173196e-01
# [3,] 2.0184237 1.5757275 -1.1317387 -8.610730e-02
# [4,] -1.3888607 0.6792888 1.2009654 -4.138688e-01
从这里,我们基本上可以将这个三维数组转换成四列data.frame
,其中三列表示坐标轴,第四列是实际值。
melted_mtx <- reshape2::melt(mtx[1,,,drop=FALSE])
str(melted_mtx, vec.len=15)
# 'data.frame': 220 obs. of 4 variables:
# $ Var1 : int 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
# $ Var2 : int 1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 ...
# $ Var3 : int 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 ...
# $ value: num 1.371 0.404 2.018 -1.389 -0.284 -0.307 1.895 0.46 1.035 -0.784 0.206 -1.368 -0.431 1.576 0.679 -0.367 -0.727 0.921 0.624 ...
因为您想在 x-axis 上绘制 myseq
,我们可以用它来替换。它在此处的第三个变量中,Var3
。我会验证它的长度是否正确,然后进行替换:
lapply(melted_mtx[-4], table)
# $Var1
# 1
# 220
# $Var2
# 1 2 3 4 5 6 7 8 9 10 11
# 20 20 20 20 20 20 20 20 20 20 20
# $Var3
# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11
melted_mtx$Var3 <- myseq[melted_mtx$Var3]
lapply(melted_mtx[-4], table)
# $Var1
# 1
# 220
# $Var2
# 1 2 3 4 5 6 7 8 9 10 11
# 20 20 20 20 20 20 20 20 20 20 20
# $Var3
# 1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96
# 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11
我们可以使用它并将其直接提供给 ggplot2
。
library(ggplot2)
ggplot(melted_mtx, aes(Var3, value, group = Var2, color = factor(Var2))) +
geom_line()
(您的代码)呈现的基本图形图:
撇开边距和颜色不谈,实际上是同一个图。可以做很多美化,包括图例标签、坐标轴等
上面,我只melt
编辑了数组的第一个平面。如果你做了整个数组,那么它看起来像这样:
melted_mtx <- reshape2::melt(mtx)
melted_mtx$Var3 <- myseq[melted_mtx$Var3]
ggplot(melted_mtx, aes(Var3, value, group = interaction(Var1, Var2),
color = interaction(Var1, Var2))) +
geom_line()
这个有点复杂,很明显,但是使用interaction
,可以按多个变量分组。这里需要分组,因为没有它 ggplot2
将尝试将所有点连接在一条线上,通常没有用。通常可以使用 just color=
来建议组,但我经常包括 group=
和 color=
以防我以后改变方式colors/linetypes/shapes/... 已定义,组被意外更改。