在所有列中绘制每一行数据(意大利面条图)
Plot each row of data across all columns (Spaghetti plot)
我有一个由 16 行和 5 列组成的 small dataset。此数据集显示在图片下方,也可以使用以下方式导入到 R:
d <- read.csv("https://raw.githubusercontent.com/izeh/i/master/De.csv")[-1]
目标: 我的目标是在基数 R 中绘制所有列中的每一行数据(意大利面图)。例如,对于 3 行(总共 16 行),我希望像下面的 图片。
问题:我试过matplot()
没有成功,有没有BASE R解决方案?
matplot(d)
整个数据:
GolBkgnd DesnTst Result PrdctConc PostBrd
1 4 4 3 5 5
2 2 3 5 3 5
3 5 4 3 5 4
4 3 4 4 1 3
5 5 3 2 4 4
6 5 4 5 5 4
7 5 3 3 1 5
8 5 4 5 5 4
9 5 3 2 1 3
10 5 4 3 4 5
11 5 3 1 4 3
12 3 4 3 4 5
13 5 3 4 2 5
14 4 4 3 5 5
15 5 3 3 5 4
16 5 4 4 3 5
这是一个使用 tidyverse
的解决方案
library(tidyverse)
d %>%
mutate(index = 1:16) %>%
gather(column, value) %>%
ggplot(aes(column, value, color = index, group = index)) +
geom_lines()
使用 base R,似乎你只需要用 t()
转置并自己绘制轴
matplot(t(d), type="l", xaxt="n")
axis(1, seq_along(d), names(d))
您正在绘制所谓的 "parallel coordinates plot"。事实上,所有的线最终都相互重叠,这让我建议使用抖动来随机地稍微分开它们:
library(MASS)
png()
parcoord( sapply(d, jitter))
dev.off()
MASS 包可以说是一个 "base"-R 包。如果需要通过颜色来识别,那么":
parcoord( sapply(d, jitter), col=1:16)
我有一个由 16 行和 5 列组成的 small dataset。此数据集显示在图片下方,也可以使用以下方式导入到 R:
d <- read.csv("https://raw.githubusercontent.com/izeh/i/master/De.csv")[-1]
目标: 我的目标是在基数 R 中绘制所有列中的每一行数据(意大利面图)。例如,对于 3 行(总共 16 行),我希望像下面的 图片。
问题:我试过matplot()
没有成功,有没有BASE R解决方案?
matplot(d)
整个数据:
GolBkgnd DesnTst Result PrdctConc PostBrd
1 4 4 3 5 5
2 2 3 5 3 5
3 5 4 3 5 4
4 3 4 4 1 3
5 5 3 2 4 4
6 5 4 5 5 4
7 5 3 3 1 5
8 5 4 5 5 4
9 5 3 2 1 3
10 5 4 3 4 5
11 5 3 1 4 3
12 3 4 3 4 5
13 5 3 4 2 5
14 4 4 3 5 5
15 5 3 3 5 4
16 5 4 4 3 5
这是一个使用 tidyverse
library(tidyverse)
d %>%
mutate(index = 1:16) %>%
gather(column, value) %>%
ggplot(aes(column, value, color = index, group = index)) +
geom_lines()
使用 base R,似乎你只需要用 t()
转置并自己绘制轴
matplot(t(d), type="l", xaxt="n")
axis(1, seq_along(d), names(d))
您正在绘制所谓的 "parallel coordinates plot"。事实上,所有的线最终都相互重叠,这让我建议使用抖动来随机地稍微分开它们:
library(MASS)
png()
parcoord( sapply(d, jitter))
dev.off()
MASS 包可以说是一个 "base"-R 包。如果需要通过颜色来识别,那么":
parcoord( sapply(d, jitter), col=1:16)